when I write a Display impl for enum, I write code without enum Type, no matter what value I give in the test, it always match the first one, why ?
I find if I specify the type Phase::Add, then it could success, but why?
this will fail the test:
#[derive(Clone, Debug)]
pub enum Phase {
Add,
Modify,
Delete,
}
impl fmt::Display for Phase {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Add => write!(f, "Add"),
Modify => write!(f, "Modify"),
Delete => write!(f, "Delete"),
_ => write!(f, "Unknown"),
}
}
}
#[test]
fn test_lifecycle_phase() {
let mut phase = Phase::Modify;
assert_eq!("Modify", phase.to_string());
phase = Phase::Delete;
assert_eq!("Delete", phase.to_string());
}
Only write like this can be ok, why?
#[derive(Clone, Debug)]
pub enum Phase {
Add,
Modify,
Delete,
}
impl fmt::Display for Phase {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Phase::Add => write!(f, "Add"),
Phase::Modify => write!(f, "Modify"),
Phase::Delete => write!(f, "Delete"),
_ => write!(f, "Unknown"),
}
}
}
#[test]
fn test_lifecycle_phase() {
let mut phase = Phase::Modify;
assert_eq!("Modify", phase.to_string());
phase = Phase::Delete;
assert_eq!("Delete", phase.to_string());
}