Enumerate without PartialEq using match
-
enum
-
match
-
dead_code
-
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:
#[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