The Rust Programming Language — Pattern Matching — Pattern matching using Enums
Enums can be used to match the pattern against literals. Literals are nothing but the variant inside the enum.
Rust provides us with a keyword called match which is basically like a switch case statement in other languages. Using match keyword, you can create blocks of code which should execute when the variable(case) matches the literal value.
Take for e.g. below code snippet:
enum Color {
Red,
Yellow,
Green
}
fn main() {
let current_color: Color = Color::Green;
match current_color {
Color::Red => {
println!("The color is Red!");
}
Color::Yellow => {
println!("The color is Yellow!");
}
Color::Green => {
println!("The color is Green!");
}
}
}Observations:
- We use the
matchkeyword on the variable/case on which we want to execute the pattern matching. - We
matchon a variable namedcurrent_colorand we create a pattern matching for eachvariantofenumColoridentifierusing namespace
Since Rust is a strict programming language, the only issue with doing pattern matching on an enum is that you need to handle each and every variant. So for the above example, you need to handle case for Color::Red, Color::yellow and Color::Green. If you do not handle each and every variants then the Rust compiler throws an error.
Take for e.g. below snippet where we do not cover Color::Green and Color::Custom{...}:
enum Color {
Red,
Yellow,
Green,
Custom { red: u8, green: u8, blue: u8 }
}
fn main() {
let current_color: Color = Color::Green;
match current_color {
Color::Red => {
println!("The color is Red!");
}
Color::Yellow => {
println!("The color is Yellow!");
}
}
}We get below compiler error:

We do need to add all the variants like what below code snippet shows:
enum Color {
Red,
Yellow,
Green,
Custom { red: u8, green: u8, blue: u8 }
}
fn main() {
let current_color: Color = Color::Green;
match current_color {
Color::Red => {
println!("The color is Red!");
}
Color::Yellow => {
println!("The color is Yellow!");
}
Color::Green => {
println!("The color is Green!");
}
Color::Custom { red, green, blue } => {
println!("The color is Custom with RGB values {}, {}, {}!", red, green, blue);
}
}
}In some cases, it may not be possible to cover each and every variants. In such case, we can use the match-all/catch-all pattern which can be achieved using _. Take for e.g. below code snippet:
enum Color {
Red,
Yellow,
Green,
Custom { red: u8, green: u8, blue: u8 }
}
fn main() {
let current_color: Color = Color::Green;
let color_description = match current_color {
Color::Red => {
"The color is Red!"
}
Color::Yellow => {
"The color is Yellow!"
}
_ => {
"The color is a different color!"
}
};
}Observations:
- We use
_for handling the default cases in other languages when using switch case - Expression/Statements syntax applies throughout the Rust Language
- We can use
matchkeyword for assigning values to variables instead ofif/elsestatements.
You can do pattern matching with structs as shown above or with tuples like below code snippet:
enum Color {
Red,
Yellow,
Green,
Custom(u8, u8, u8)
}
fn main() {
let current_color: Color = Color::Green;
match current_color {
Color::Red => {
println!("The color is Red!");
}
Color::Yellow => {
println!("The color is Yellow!");
}
Color::Green => {
println!("The color is Green!");
}
Color::Custom(red, green, blue) => {
println!("The color is Custom with RGB values {}, {}, {}!", red, green, blue);
}
}
}When using _, there is a small catch here. If you use _, Rust compiler stops giving you an error saying you missed a certain condition like Color::green. This can be a drawback when you plan to introduce a new variant to your enum identifier.
Over all, the match keyword is a great replacement for if/else condition for assigning values to a variable. Also the strictness of the Rust compiler allows you to not miss a variant to be handled.
I hope you enjoyed this article about pattern matching. Please share your feedback in the comments section.
You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.






