There are a few ways to define an enum variant and, as a result, to use it when creating match expressions. We can define a variant in three ways – one-word, Tuple-like, or Struct-like.
Match One-Word Enum Variants
Let’s say we want an enum based on the days of a week, as shown below. Each variant is case-sensitive and represents a day in a week.
1 2 3 4 5 6 7 8 9 | enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } |
To use the variants with the match keyword, we construct match expressions as follows. There are only six match expressions for seven days – one expression matches for Saturday or Sunday!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | fn main() { check_date(Days::Tuesday); check_date(Days::Sunday); check_date(Days::Saturday); } fn check_date(day: Days) { match day { Days::Monday => println!("It's Monday!"), Days::Tuesday => println!("It's Tuesday!"), Days::Wednesday => println!("It's Wednesday!"), Days::Thursday => println!("It's Thursday!"), Days::Friday => println!("It's Friday!"), Days::Saturday | Days::Sunday => println!("It's weekend!") } } |
We don’t have many options for this type of variant. For each expression, we can only use the variant itself or the OR (|) operator to specify multiple variants.
The codes output the following.
1 2 3 | It's Tuesday! It's weekend! It's weekend! |
Match Tuple-Like Enum Variants
We can also create enum variants like tuples and use them in match expressions differently from the previous examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | fn main() { check_date(MONTHS::April(4, 1980)); check_date(MONTHS::April(4, 2020)); check_date(MONTHS::April(1, 1950)); check_date(MONTHS::September(1, 2020)); check_date(MONTHS::November(4, 2019)); check_date(MONTHS::December(5, 2018)); } enum MONTHS { January(u8, u16), February(u8, u16), March(u8, u16), April(u8, u16), May(u8, u16), June(u8, u16), July(u8, u16), August(u8, u16), September(u8, u16), October(u8, u16), November(u8, u16), December(u8, u16) } fn check_date(date: MONTHS) { match date { MONTHS::April(x, 1980) => println!("April {} - Any day in April 1980", x), MONTHS::April(1, x) => println!("It's April Fool's day!"), MONTHS::December(5, 2018) => println!("Extract Date"), MONTHS::September(x, y) =>println!("September {} {}", x, y), MONTHS::February(x, y) | MONTHS::March(x, y) => println!("Nobody likes February and March!"), _ => println!("Unknown Date") } } |
The codes output the following.
1 2 3 4 5 6 | April 4 - Any day in April 1980 Unknown Date It's April Fool's day! September 1 2020 Unknown Date Extract Date |
Struct-like Enum Variants
Another way to create variants is by defining them like structs. How we use them with match expressions is somewhat similar to tuple-like variants.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | enum MONTHS { January{ day: u8, year: u16 }, February{ day: u8, year: u16 }, March{ day: u8, year: u16 }, April{ day: u8, year: u16 }, May{ day: u8, year: u16 }, June{ day: u8, year: u16 }, July{ day: u8, year: u16 }, August{ day: u8, year: u16 }, September{ day: u8, year: u16 }, October{ day: u8, year: u16 }, November{ day: u8, year: u16 }, December{ day: u8, year: u16, location: String }, } |
Each struct-like variant can vary from the other variants.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | fn main() { check_date(MONTHS::April{ day: 4, year: 1980}); check_date(MONTHS::April{ day: 5, year: 1980}); check_date(MONTHS::April{day: 1, year: 1950}); check_date(MONTHS::September{day: 1, year: 2020}); check_date(MONTHS::November{day: 4, year: 2019}); check_date(MONTHS::December{day: 5, year: 2018, location: String::from("Philippines")}); } fn check_date(date: MONTHS) { match date { MONTHS::April{day: x, year: 1980} => println!("April {} - Any day in April 1980", x), MONTHS::April{day: 1, year: x} => println!("It's April Fool's day!"), MONTHS::December{day: 5, year: 2018, location: z} => println!("Extract Date in {}", z), MONTHS::September{day: x, year: y} =>println!("September {} {}", x, y), MONTHS::February{day: x, year: y} | MONTHS::March{day: x, year: y} => println!("Nobody likes February and March!"), _ => println!("Unknown Date") } } |
The codes output the following.
1 2 3 4 5 6 | April 4 - Any day in April 1980 April 5 - Any day in April 1980 It's April Fool's day! September 1 2020 Unknown Date Extract Date in Philippines |
Mixed-Style Variants
In Rust, we can use different styles of variant creation within the same enum. Consider the following codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | enum MONTHS { January, February{ day: u8, year: u16 }, March{ day: u8, year: u16 }, April{ day: u8, year: u16 }, May{ day: u8, year: u16 }, June{ day: u8, year: u16 }, July{ day: u8, year: u16 }, August{ day: u8, year: u16 }, September{ day: u8, year: u16 }, October (u8, u16), November{ day: u8, year: u16 }, December{ day: u8, year: u16, location: String }, } |
Now we have a one-word January, the tuple-like October, and the rest of the variants are struct in form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | fn main() { check_date(MONTHS::October(1, 1800)); check_date(MONTHS::January); check_date(MONTHS::April{ day: 4, year: 1980}); check_date(MONTHS::April{ day: 5, year: 1980}); check_date(MONTHS::April{day: 1, year: 1950}); check_date(MONTHS::September{day: 1, year: 2020}); check_date(MONTHS::November{day: 4, year: 2019}); check_date(MONTHS::December{day: 5, year: 2018, location: String::from("Philippines")}); } fn check_date(date: MONTHS) { match date { MONTHS::April{day: x, year: 1980} => println!("April {} - Any day in April 1980", x), MONTHS::April{day: 1, year: x} => println!("It's April Fool's day!"), MONTHS::December{day: 5, year: 2018, location: z} => println!("Extract Date in {}", z), MONTHS::September{day: x, year: y} =>println!("September {} {}", x, y), MONTHS::February{day: x, year: y} | MONTHS::March{day: x, year: y} => println!("Nobody likes February and March!"), MONTHS::January => println!("First month of the year"), MONTHS::October(x, y) => println!("October {}, {}", x, y), _ => println!("Unknown Date") } } |
The codes output the following.
1 2 3 4 5 6 7 8 | October 1, 1800 First month of the year April 4 - Any day in April 1980 April 5 - Any day in April 1980 It's April Fool's day! September 1 2020 Unknown Date Extract Date in Philippines |
We tested the codes using Rust 1.42.0.