- enum
- match
- dead_code
Enumerate without PartialEq using match
- If we don't have PartialEq on an enum and we don't want to add it or cannot add it (e.g. because it was supplied by an external crate) we can use match:
examples/enums/colors-match/src/main.rs
#[allow(dead_code)] enum ColorName { Red, Green, Blue, White, Black, } fn main() { let background = ColorName::Black; let foreground = ColorName::White; let ink = ColorName::Black; let frame = ColorName::Red; for color in [background, foreground, ink, frame] { match color { ColorName::White => println!("white: #FFFFFF"), ColorName::Red => println!("red: #FF0000"), _ => println!("other"), } } }
other white: #FFFFFF other red: #FF0000