1

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());
}

wonderflow
  • 665
  • 2
  • 7
  • 18

1 Answers1

3

This is a common pitfall. It's because Add => ... does not refer to Phase::Add but binds the value of self to the new variable Add. The name Add is just coincidental, yet the pattern Phase::Add and the (new) variable Add are completely different constructs. Since the variable Add has no pattern restrictions it always matches; the variables Modify and Delete remain unused.

As you found out yourself, you need to use Phase::Add, not Add.

trent
  • 25,033
  • 7
  • 51
  • 90
user2722968
  • 13,636
  • 2
  • 46
  • 67