The match expression helps you check a value against many possibilities and run code based on which one fits. It makes your code clear and easy to read.
When you want to do different things based on different values of a variable.
When you have a few fixed options and want to handle each one clearly.
When you want to replace many <code>if-else</code> checks with cleaner code.
When you want to make sure you cover all possible cases of a value.
When you want to extract data from complex values and use it.
Syntax
Rust
match value {
pattern1 => expression1,
pattern2 => expression2,
_ => default_expression,
}
The value is what you want to check.
Each pattern is a possible match. The arrow => points to the code to run if it matches.
Examples
This checks the number and prints its name or "Other number" if no match.
Rust
let number = 3;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Other number"),
}
This matches string values and prints a message for each color.
Rust
let color = "red";
match color {
"red" => println!("Stop"),
"green" => println!("Go"),
"yellow" => println!("Slow down"),
_ => println!("Unknown color"),
}
This matches enum variants and prints a direction message.
Rust
enum Direction {
North,
South,
East,
West,
}
let dir = Direction::East;
match dir {
Direction::North => println!("Going up"),
Direction::South => println!("Going down"),
Direction::East => println!("Going right"),
Direction::West => println!("Going left"),
}
Sample Program
This program uses match to find the name of the day from its number and prints it.
Rust
fn main() {
let day = 3;
let day_name = match day {
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",
_ => "Invalid day",
};
println!("Day {} is {}", day, day_name);
}
OutputSuccess
Important Notes
The _ pattern is a catch-all for any value not matched earlier.
Each match arm must return the same type if you use match as an expression.
You can match complex patterns like enums, tuples, and even use guards for extra checks.
Summary
Match checks a value against many patterns clearly.
It helps replace many if-else statements with cleaner code.
Always include a catch-all _ to handle unexpected cases.
Practice
(1/5)
1. What is the main purpose of the match statement in Rust?
easy
A. To check a value against multiple patterns clearly and concisely
B. To create loops that run a fixed number of times
C. To define new data types
D. To handle errors using exceptions
Solution
Step 1: Understand the role of match
The match statement compares a value against many patterns, making code clearer than many if-else statements.
Step 2: Compare with other options
Loops run repeatedly, data types define structures, and exceptions handle errors, but match is for pattern matching.
Final Answer:
To check a value against multiple patterns clearly and concisely -> Option A
Quick Check:
Match = pattern checking [OK]
Hint: Match replaces many if-else with clear pattern checks [OK]
Common Mistakes:
Confusing match with loops
Thinking match defines types
Assuming match handles exceptions
2. Which of the following is the correct syntax for a match expression in Rust?
easy
A. match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr }
B. match (value) { pattern1: expr1; pattern2: expr2; default: default_expr }
C. match value { pattern1 -> expr1, pattern2 -> expr2, _ -> default_expr }
D. match value { pattern1 => expr1; pattern2 => expr2; _ => default_expr }
Solution
Step 1: Recall Rust match syntax
Rust uses curly braces with patterns followed by => and expressions separated by commas.
Step 2: Check each option
match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr } uses correct braces, arrows, and commas. Options B and D use semicolons incorrectly. match value { pattern1 -> expr1, pattern2 -> expr2, _ -> default_expr } uses -> instead of =>.
Final Answer:
match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr } -> Option A
Quick Check:
Correct match syntax uses => and commas [OK]
Hint: Use => between pattern and expression, separate arms with commas [OK]
Common Mistakes:
Using semicolons instead of commas
Using -> instead of =>
Missing the catch-all _ pattern
3. What will be the output of this Rust code?
let number = 3;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Other"),
}
medium
A. One
B. Two
C. Other
D. Three
Solution
Step 1: Identify the matched value
The variable number is 3, so the match will look for pattern 3.
Step 2: Find the matching arm
The arm 3 => println!("Three") matches and prints "Three".
Final Answer:
Three -> Option D
Quick Check:
number=3 matches 3 => "Three" [OK]
Hint: Match value exactly to print correct output [OK]
Common Mistakes:
Choosing default _ arm instead of exact match
Confusing numbers with strings
Ignoring the value of number
4. Identify the error in this Rust match code snippet:
let x = 5;
match x {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three")
}
medium
A. Missing semicolon after println! macros
B. Using parentheses instead of curly braces in match
C. Missing catch-all _ pattern to handle other values
D. Incorrect variable declaration
Solution
Step 1: Check match arms completeness
The match covers 1, 2, and 3 but does not handle other values like 5.
Step 2: Understand Rust match requirements
Rust requires all possible values to be covered, usually by a catch-all _ pattern.
Final Answer:
Missing catch-all _ pattern to handle other values -> Option C
Quick Check:
Always include _ to cover unmatched cases [OK]
Hint: Always add _ arm to handle unexpected values [OK]