Introduction
match is Rust's powerful pattern-matching control flow construct. It compares a value against a series of patterns and executes the code associated with the first matching pattern. Unlike switch statements in many other languages, match in Rust is exhaustive: the compiler requires every possible value of the matched type to be covered, either explicitly or through a catch-all _ pattern, which eliminates an entire class of bugs from forgotten cases.
Cricket analogy: match is like an umpire checking a dismissal against every possible mode — bowled, caught, lbw, run-out — and Rust's compiler insists every mode be covered (or a catch-all "_") before the decision is accepted, unlike a casual switch-style scorecard that might silently miss a rare dismissal type.
Syntax
match value {
pattern1 => expression1,
pattern2 => expression2,
_ => default_expression,
}
// as an expression producing a value
let result = match value {
pattern1 => expr1,
_ => expr2,
};Explanation
Each arm of a match consists of a pattern and an expression separated by =>, with arms separated by commas. match can match literal values, ranges (like 1..=5), tuples, and enum variants, and it can destructure structs and enums to bind their inner fields to new variables within the arm. Because match is itself an expression, it can return a value, and just like if-else, all arms must evaluate to the same type when used this way. The exhaustiveness check is performed at compile time — if you match on an enum and add a new variant later without updating the match, compilation fails until you handle the new case.
Cricket analogy: Each match arm pairs a dismissal pattern with an outcome via =>, like matching literal scores, run ranges (1..=5), or destructuring a Wicket enum variant to bind the bowler's name; since match is an expression, every arm must return the same result-type, and adding a new dismissal variant later (like "obstructing the field") breaks compilation until every match handles it.
Example
enum Shape {
Circle(f64),
Rectangle(f64, f64),
Triangle { base: f64, height: f64 },
}
fn area(shape: &Shape) -> f64 {
match shape {
Shape::Circle(radius) => std::f64::consts::PI * radius * radius,
Shape::Rectangle(w, h) => w * h,
Shape::Triangle { base, height } => 0.5 * base * height,
}
}
fn main() {
let n = 7;
let description = match n {
0 => "zero",
1..=5 => "small",
6..=10 => "medium",
_ => "large",
};
println!("{} is {}", n, description);
let c = Shape::Circle(2.0);
println!("area = {:.2}", area(&c));
}Output
7 is medium
area = 12.57Key Takeaways
- match is exhaustive — the compiler forces you to cover every possible case or use a _ wildcard.
- Arms use the pattern => expression, syntax and can bind variables from the matched value.
- match can match literals, ranges, tuples, and enum variants, including destructuring.
- match is an expression and can produce a value, with all arms needing the same type.
- Adding a new enum variant later causes a compile error at every non-exhaustive match on it, aiding refactoring safety.
Practice what you learned
1. What does the Rust compiler require of a match expression?
2. Which pattern acts as a catch-all in a match expression?
3. In `match n { 1..=5 => "small", _ => "other" }`, what does 1..=5 represent?
4. What happens if you add a new variant to an enum but forget to update an existing exhaustive match on it?
Was this page helpful?
You May Also Like
if-else Expressions in Rust
Learn how Rust's if-else works as an expression, requires bool conditions, and can return values directly.
if let and while let in Rust
Simplify single-pattern matching in Rust using the concise if let and while let constructs.
Enums in Rust
Enums define a type by enumerating its possible variants, each of which can optionally hold its own data.
The Option Type in Rust
Understand how Rust's Option<T> enum replaces null to represent optional values safely.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics