Match Statement
Part of the Fundamentals section of Coddy's Rust journey — lesson 26 of 75.
The match expression allows you to compare a value against a series of patterns and execute code based on which pattern matches.
Here's the basic structure of a match expression:
match variable {
pattern1 => expression1,
pattern2 => expression2,
// ... more patterns
_ => default_expression,
}The match keyword is followed by the value you want to test.
Each arm of the match consists of a pattern followed by => and the code to execute.
The underscore _ is the default case that matches anything not matched by other patterns.
Here's an example:
let day = 3;
let day_name = match day {
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
// ... more patterns,
_ => "Invalid day",
};For multiple lines of code in an arm, use a block:
let day = 3;
let day_name = match day {
1 => {
println!("First day of the week!");
"Monday"
},
2 => "Tuesday",
// ... other cases
_ => "Invalid day",
};You can match multiple patterns using |:
let day = 3;
let day_type = match day {
1 | 2 | 3 | 4 | 5 => "Weekday",
6 | 7 => "Weekend",
_ => "Invalid day",
};Match expressions in Rust must be exhaustive, meaning they must cover all possible values. The compiler will check this for you.
Challenge
BeginnerCreate a program that takes a month number (1 for January, 2 for February, etc.) and prints the season it belongs to. Use a match statement for the logic.
The seasons and their corresponding months are:
- Winter: December (12), January (1), February (2)
- Spring: March (3), April (4), May (5)
- Summer: June (6), July (7), August (8)
- Autumn: September (9), October (10), November (11)
Cheat sheet
The match expression compares a value against patterns and executes code based on which pattern matches:
match variable {
pattern1 => expression1,
pattern2 => expression2,
_ => default_expression,
}Basic example:
let day = 3;
let day_name = match day {
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
_ => "Invalid day",
};For multiple lines of code in an arm, use a block:
let day = 3;
let day_name = match day {
1 => {
println!("First day of the week!");
"Monday"
},
2 => "Tuesday",
_ => "Invalid day",
};Match multiple patterns using |:
let day = 3;
let day_type = match day {
1 | 2 | 3 | 4 | 5 => "Weekday",
6 | 7 => "Weekend",
_ => "Invalid day",
};Match expressions must be exhaustive - they must cover all possible values. The underscore _ is the default case that matches anything not matched by other patterns.
Try it yourself
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let month: i32 = input.trim().parse().unwrap();
// Write your code below
let season =
// Don't change the line below
println!("status = {}", season);
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input