Enumeration with non-exhaustive patterns
-
enum
-
dead_code
-
In this example in the
match
we don't hanle every variant of the enum and thus we have to handle the "deafult" case using and_
underscore. -
Try running this code after commenting out the row handline
_
.
#[derive(Debug)] #[allow(dead_code)] enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, } // We need the allow dead_code beacause in this example // we did not use all the values that were listed in the enum fn main() { let today = Weekday::Sunday; println!("{:?}", today); println!(); for day in [ Weekday::Monday, Weekday::Tuesday, Weekday::Saturday, Weekday::Sunday, ] { println!("{:?}", day); match day { Weekday::Sunday => println!("Today is {day:?}, it is a day off in Europe"), Weekday::Saturday => println!("Today is {day:?}, it is a day off in Israel"), _ => println!("Today is {day:?}, it is a workday"), } } }
Sunday
Monday
Today is Monday, it is a workday
Tuesday
Today is Tuesday, it is a workday
Saturday
Today is Saturday, it is a day off in Israel
Sunday
Today is Sunday, it is a day off in Europe