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.
More Related Contents:
- What are Rust’s exact auto-dereferencing rules?
- Why is a trait not implemented for a type that clearly has it implemented?
- How can I include a module from another file from the same project?
- What does “Sized is not implemented” mean?
- How do I implement a trait I don’t own for a type I don’t own?
- How do I implement Copy and Clone for a type that contains a String (or any type that doesn’t implement Copy)?
- What makes something a “trait object”?
- Is it possible to use `impl Trait` as a function’s return type in a trait definition?
- Cannot borrow as immutable because it is also borrowed as mutable in function arguments
- What is the relation between auto-dereferencing and deref coercion?
- What is the return type of the indexing operation?
- How to do a binary search on a Vec of floats?
- Change enum variant while moving the field to the new variant
- I implemented a trait for another trait but cannot call methods from both traits
- When is it useful to define multiple lifetimes in a struct?
- Can I write an Iterator that mutates itself and then yields a reference into itself?
- Moved variable still borrowing after calling `drop`?
- What is the difference between Copy and Clone?
- Is it possible to have stack allocated arrays with the size determined at runtime in Rust?
- Temporary value is freed at the end of this statement [duplicate]
- How can I co-sort two Vecs based on the values in one of the Vecs?
- Is there a way to combine multiple traits in order to define a new trait? [duplicate]
- How can I store a pattern in a variable in Rust?
- How to switch between Rust toolchains?
- What does “missing lifetime specifier” mean when storing a &str in a structure?
- What is the difference between immutable and const variables in Rust?
- What is the purpose of the unit type in Rust?
- Why does the Rust compiler request I constrain a generic type parameter’s lifetime (error E0309)?
- Cannot pass self as callback parameter due to double borrowing
- How do I create a HashMap with type erased keys?