The match expression in Rust allows you to compare a value against a series of patterns and execute code based on which pattern matches. It's similar to a switch statement but more powerful and exhaustive.
Click to reveal answer
beginner
How does Rust ensure all cases are handled in a match expression?
Rust requires that match expressions be exhaustive, meaning all possible cases must be covered. If not, the compiler will give an error. You can use a catch-all pattern _ to handle any remaining cases.
Click to reveal answer
beginner
What is the purpose of the underscore _ pattern in a match?
The underscore _ pattern acts as a wildcard that matches any value not matched by previous arms. It is used as a catch-all to ensure exhaustiveness without specifying every possible value.
Click to reveal answer
intermediate
Can match arms bind variables? Give an example.
Yes, match arms can bind variables to parts of the matched value. For example:
match some_option {
Some(x) => println!("Value is {}", x),
None => println!("No value"),
}
Click to reveal answer
beginner
What happens if no match arm matches and there is no catch-all?
The Rust compiler will produce an error because match expressions must be exhaustive. You must handle all possible cases or include a catch-all _ arm.
Click to reveal answer
What keyword is used in Rust to compare a value against multiple patterns?
Amatch
Bswitch
Ccase
Dselect
✗ Incorrect
Rust uses the match keyword to compare a value against patterns.
What does the underscore _ pattern represent in a match?
AA variable binding
BA wildcard matching any value
CA specific value
DAn error case
✗ Incorrect
The underscore _ is a wildcard pattern that matches any value not matched before.
What will happen if a match expression is not exhaustive?
AIt will ignore unmatched cases silently
BIt will run but skip unmatched cases
CIt will panic at runtime
DIt will cause a compiler error
✗ Incorrect
Rust requires match expressions to be exhaustive; otherwise, the compiler throws an error.
Can you bind variables inside match arms?
AYes, to parts of the matched value
BOnly in the catch-all arm
COnly constants can be used
DNo, variables cannot be bound
✗ Incorrect
You can bind variables to parts of the matched value inside match arms.
Which of these is a valid match arm pattern?
Acase x
Bdefault
CSome(x)
Dswitch(x)
✗ Incorrect
Some(x) is a valid pattern matching an enum variant with a variable binding.
Explain how the match expression works in Rust and why it is useful.
Think about how you decide what to do based on different cases.
You got /4 concepts.
Describe the role of the underscore _ pattern in a match expression.
It’s like saying 'anything else' in a list of options.
You got /4 concepts.
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]