`match`
match chooses a branch by pattern and demands that every possibility is covered. It is the usual way to open an enum and the compiler's exhaustiveness check.
Detailed Explanation
match status { Ready => ..., Blocked => ..., } looks like a switch. It is stricter. Every variant of the scrutinee's type must appear, or you must write a catch-all _ or other. If someone adds a variant to the enum tomorrow, this match fails to compile until you handle it. That is the feature. A switch in C that forgets a case falls through or does nothing; a Rust match will not let you ship that.
The thing you match on is the scrutinee. Each arm is a pattern, an optional if guard, and a body. Patterns can bind names: match maybe { Some(user) => user.id, None => 0 }. They can nest: Some(Err(e)). They can destructure structs and tuples. The first matching arm wins, so put specific arms before _.
match is an expression. All arms must have the same type, and that is the type of the match. This is how you write a total function from an enum to a value without an uninitialized hole. if plus else if can do the same job for booleans; it cannot prove you handled every variant of an enum.
Guards let you add a boolean without leaving match: Some(n) if n > 10 => .... A guard that fails does not bind; the next arm is tried. That means two Some(n) arms with different guards are fine.
You can match on integers, booleans, characters, strings, and ranges (0..=9). Those are not enums, so the compiler cannot list every value; you must include a _ arm. Exhaustiveness is complete for enums, bool, and the unit type; it is "you wrote a catch-all" for open types.
What it costs: a match on an enum is usually a jump table or a compare chain, no more expensive than the equivalent if. Matching on a String compares contents. Do not match on a String when an enum would do; parse once, match the enum forever after.
When you reach for match: any enum, any Result or Option you need to handle fully, any time a forgotten case would be a bug. When you only care about one variant, if let is less noisy. When you want to keep looping while a pattern matches, while let.
Code Examples
enum Status {
Ready,
Blocked { retries: u8 },
Failed(String),
}
fn label(status: Status) -> String {
match status {
Status::Ready => "ok".into(),
Status::Blocked { retries } if retries < 3 => format!("retry {retries}"),
Status::Blocked { .. } => "exhausted".into(),
Status::Failed(reason) => format!("fail {reason}"),
}
}
fn main() {
println!("{}", label(Status::Blocked { retries: 1 })); // prints: retry 1
println!("{}", label(Status::Blocked { retries: 5 })); // prints: exhausted
}Common Gotchas
A missing variant is a compile error
Add the arm, or use `_` if you truly want to ignore new variants. `_` silences the exhaustiveness check you usually want.
First match wins
A `_` arm above a specific variant makes the specific arm unreachable. Order from most specific to least.
Guards do not count for exhaustiveness
`Some(n) if n > 0` does not cover `Some(0)`. You still need an unguarded `Some(_)` or `_`.
Matching a `String` is a content compare
Parse into an enum at the boundary and match the enum. Do not sprinkle string matches through the program.