Match basic values
Quick answer
match compares a value against patterns and executes the first matching arm. It is useful when several distinct values or ranges need different behavior. The compiler requires the arms to cover every possible input value.
Patterns and exhaustive coverage
Write each arm as pattern => expression,. The wildcard _ matches anything not handled by earlier arms. Use | between alternative patterns and ..= for an inclusive range. Like if, match can yield a value; the arm results must be type-compatible.
fn main() { let choice = 2; let message = match choice { 1 => "Read", 2 => "Write", _ => "Unknown", }; println!("{message}");}Expected stdout (not executed):
WriteThe literal pattern 2 selects the second arm.
fn main() { let day = 6; let kind = match day { 1..=5 => "Weekday", 6 | 7 => "Weekend", _ => "Invalid day", }; println!("{kind}");}Expected stdout (not executed):
WeekendEither 6 or 7 matches the second arm.
Common mistake
Do not place _ before specific patterns that you intend to use; it matches all values and makes later patterns unreachable.
Try it yourself
Map 0 to Empty, 1 through 5 to Low, and all other integers to Other.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Match basic values — practice quiz
Choose one answer per question, then review the explanation.
What is returned by `match 4 { 1..=4 => "low", _ => "other" }`?
Why put `_` after the specific patterns in a match?
Can `match true { true => 1, false => 2 }` omit the wildcard?