Matching on Enum Variants
Use match to handle each case.
Matching on Enum Variants is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet the match Expression
The match expression compares a value against a series of patterns and runs the code for the first one that fits.
It is the natural partner of enums: you can handle each variant in its own arm.
enum Light {
Red,
Green,
}
fn action(l: Light) {
match l {
Light::Red => println!("Stop"),
Light::Green => println!("Go"),
}
}Anatomy of a match Arm
Each arm has the form pattern => expression, and arms are separated by commas.
The pattern is what to match, and the expression after => runs when it matches. Here the pattern is a single enum variant.
match light {
Light::Red => println!("Stop"),
Light::Green => println!("Go"),
}A Full Matching Program
Here is a complete program that matches on a traffic light and prints an instruction.
Run it, then change the variant in main to watch a different arm fire.
enum TrafficLight {
Red,
Yellow,
Green,
}
fn main() {
let light = TrafficLight::Yellow;
match light {
TrafficLight::Red => println!("Stop"),
TrafficLight::Yellow => println!("Slow down"),
TrafficLight::Green => println!("Go"),
}
}match Must Be Exhaustive
Rust requires match to cover every possible variant. If you forget one, the program will not compile.
This is a powerful safety feature: add a new variant later and the compiler reminds you about every place that must handle it.
// This would FAIL to compile because
// the Green case is missing:
// match light {
// Light::Red => println!("Stop"),
// }The Catch-All with _
When you do not want to list every variant, use the underscore _ as a catch-all pattern. It matches anything not handled above.
Place it last, since arms are tried in order.
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn main() {
let c = Coin::Dime;
match c {
Coin::Penny => println!("1 cent"),
_ => println!("Some other coin"),
}
}match Returns a Value
match is an expression, so it produces a value you can store in a variable.
Every arm must return the same type. Notice there is no semicolon after each arm's expression here.
enum Coin {
Penny,
Quarter,
}
fn main() {
let coin = Coin::Quarter;
let cents = match coin {
Coin::Penny => 1,
Coin::Quarter => 25,
};
println!("{} cents", cents);
}Multi-Line Arm Bodies
An arm can run several statements by wrapping them in curly braces { ... }.
The last expression inside the braces is the value the arm produces.
match light {
Light::Red => {
println!("Caution!");
println!("Stop the car");
}
Light::Green => println!("Go"),
}Combining Patterns with |
Use the pipe | to match several variants in one arm. It reads as "or".
This avoids repeating the same body for variants that should behave identically.
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn main() {
let c = Coin::Nickel;
match c {
Coin::Penny | Coin::Nickel => println!("Small coin"),
_ => println!("Bigger coin"),
}
}match Versus if/else
You could chain if / else if to check variants, but match is clearer and safer.
With match, the compiler verifies you handled every case. A chain of if statements gives no such guarantee.
Matching and Printing Together
A common pattern is to turn a variant into a friendly string and print it.
Run this program and change the chosen direction to see the matching message.
enum Direction {
North,
South,
East,
West,
}
fn main() {
let d = Direction::West;
let name = match d {
Direction::North => "North",
Direction::South => "South",
Direction::East => "East",
Direction::West => "West",
};
println!("Heading {}", name);
}Order Matters
Arms are checked from top to bottom, and the first match wins. The rest are skipped.
This is why the catch-all _ must come last: if it were first, it would swallow every value before the specific arms could run.
Quick Check
Check your understanding of match.
Recap
You learned that match compares a value against patterns and runs the first matching arm.
It must be exhaustive, can use _ as a catch-all, can combine variants with |, and returns a value because it is an expression. Next you will give enum variants their own data.
Frequently asked questions
Is the “Matching on Enum Variants” lesson free?
Yes — the full text of “Matching on Enum Variants” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Matching on Enum Variants”?
Use match to handle each case. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Matching on Enum Variants” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining Your First Enum
- Matching on Enum Variants
- Enums with Data
- Match Guards and Bindings