What is the syntax to match on a reference to an enum?

Edit: Please see Shepmaster’s answer for the latest idiom

The idiomatic way would be

match *self {
    Animal::Cat(ref c) => f.write_str("c"),
    Animal::Dog => f.write_str("d"),
}

You can use _ instead of ref c to silence the “unused” warning.

Leave a Comment