A match expression lets you compare a value against many patterns and execute code based on which pattern matches. It's like a smart switch that checks all possibilities.
Click to reveal answer
beginner
How does Rust handle unmatched patterns in a match expression?
Rust requires all possible cases to be covered in a match. If some cases are missing, the code won't compile. You can use a catch-all pattern _ to cover any leftover cases.
Click to reveal answer
intermediate
What is the role of the @ operator in a match pattern?
The @ operator lets you name a value while also testing it against a pattern. For example, id @ 1..=5 means "match values from 1 to 5 and call it id".
Click to reveal answer
beginner
Explain how to match multiple patterns in one arm of a match expression.
You can match multiple patterns separated by | in one arm. For example, 1 | 2 | 3 => println!("One to three") runs the same code for 1, 2, or 3.
Click to reveal answer
beginner
What happens if you forget to handle a pattern in a match expression?
Rust will give a compile-time error saying the match is not exhaustive. This helps avoid bugs by forcing you to handle all possible cases.
Click to reveal answer
What does the underscore _ pattern do in a match expression?
AMatches only negative numbers
BMatches any value not matched by previous patterns
CMatches only zero
DCauses a compile error
✗ Incorrect
The underscore _ is a catch-all pattern that matches any value not matched before.
How do you bind a matched value to a variable inside a match arm?
AYou cannot bind values in <code>match</code>
BUsing <code>let</code> inside the arm
CUsing <code>match</code> keyword again
DUsing the <code>@</code> operator, like <code>id @ 1..=5</code>
✗ Incorrect
The @ operator lets you name the matched value while testing it against a pattern.
Which of these is a valid way to match multiple values in one match arm?
A1 | 2 | 3 => println!("Matched")
B1 & 2 & 3 => println!("Matched")
C1, 2, 3 => println!("Matched")
D1 + 2 + 3 => println!("Matched")
✗ Incorrect
The pipe | operator matches any one of the listed patterns.
What will happen if a match expression does not cover all possible cases?
ACompile-time error
BRuntime panic
CIt will ignore missing cases
DIt will print a warning but compile
✗ Incorrect
Rust enforces exhaustive matching at compile time to prevent bugs.
In a match expression, what does this arm do? Some(x) => println!("Value: {}", x)
ACauses a syntax error
BMatches any value and ignores it
CMatches an Option with a value and binds it to x
DMatches None variant
✗ Incorrect
This matches the Some variant of Option and binds the inner value to x.
Explain how Rust's match expression ensures all cases are handled and why this is helpful.
Think about what happens if you forget a case.
You got /4 concepts.
Describe how to use the @ operator in a match pattern and give an example.
It lets you name the value while matching it.
You got /3 concepts.
Practice
(1/5)
1. What does the match expression do in Rust?
easy
A. It loops over a range of numbers.
B. It creates a new variable with a fixed value.
C. It defines a function with multiple parameters.
D. It compares a value against multiple patterns and executes matching code.
Solution
Step 1: Understand the purpose of match
The match expression is used to compare a value against several patterns.
Step 2: Identify what happens on a match
When a pattern matches, the corresponding code block runs, making match a clear way to handle multiple cases.
Final Answer:
It compares a value against multiple patterns and executes matching code. -> Option D
Quick Check:
Match expression = pattern matching [OK]
Hint: Match compares values to patterns and runs matching code [OK]
Common Mistakes:
Thinking match creates variables
Confusing match with loops
Assuming match defines functions
2. Which of the following is the correct syntax for a simple match expression in Rust?
easy
A. match value { 1 => println!("One") else println!("Other") }
B. match value { 1 => println!("One"), _ => println!("Other") }
C. match value { 1 -> println!("One"), _ -> println!("Other") }
D. match (value) { 1: println!("One"), _ : println!("Other") }
Solution
Step 1: Recall Rust match syntax
Rust uses curly braces with patterns followed by => and the code to run.
Step 2: Check each option
match value { 1 => println!("One"), _ => println!("Other") } uses correct syntax with => and a default case using _; others use invalid symbols like : or -> or else.
Final Answer:
match value { 1 => println!("One"), _ => println!("Other") } -> Option B
Quick Check:
Correct match syntax uses => and _ for default [OK]
Hint: Use => for patterns and _ for default case in match [OK]
Common Mistakes:
Using : or -> instead of =>
Omitting commas between arms
Using else instead of _ for default
3. What is the output of this Rust code?
let x = Some(5);
match x {
Some(n) if n > 3 => println!("Greater than 3: {}", n),
Some(n) => println!("Less or equal to 3: {}", n),
None => println!("No value"),
}
medium
A. Greater than 3: 5
B. Less or equal to 3: 5
C. No value
D. Compilation error
Solution
Step 1: Analyze the match arms and value
The value is Some(5). The first arm matches Some(n) with a guard n > 3.
Step 2: Check the guard condition
Since 5 > 3 is true, the first arm runs and prints "Greater than 3: 5".
Final Answer:
Greater than 3: 5 -> Option A
Quick Check:
Guard true triggers first arm output [OK]
Hint: Check if guards are true to pick correct match arm [OK]
Common Mistakes:
Ignoring the if guard condition
Choosing second arm without checking guard
Thinking None matches Some value
4. Identify the error in this Rust match expression:
let num = 2;
match num {
1 => println!("One"),
2 => println!("Two")
3 => println!("Three"),
_ => println!("Other"),
}
medium
A. Match arms must be in parentheses, not braces.
B. Using _ as default case is invalid.
C. Missing comma after the second arm (2 => ...).
D. Cannot match integer values directly.
Solution
Step 1: Check syntax of each match arm
Each arm must end with a comma except possibly the last. The second arm lacks a comma after println!("Two").
Step 2: Validate other parts
Using _ is valid for default, braces are correct, and matching integers is allowed.
Final Answer:
Missing comma after the second arm (2 => ...). -> Option C
Quick Check:
Commas separate match arms [OK]
Hint: Check commas after each match arm carefully [OK]
Common Mistakes:
Forgetting commas between arms
Misunderstanding _ as invalid
Confusing braces with parentheses
5. Given a list of Option<i32> values, how can you use a match expression inside a loop to print only the positive numbers, ignoring None and non-positive values?
Choose the correct code snippet.
hard
A. for val in list {
match val {
Some(n) if n > 0 => println!("Positive: {}", n),
_ => (),
}
}
B. for val in list {
match val {
Some(n) => println!("Positive: {}", n),
None => println!("No value"),
}
}
C. for val in list {
if let Some(n) = val {
if n > 0 {
println!("Positive: {}", n);
}
}
}
D. for val in list {
match val {
Some(n) if n <= 0 => println!("Positive: {}", n),
_ => (),
}
}
Solution
Step 1: Understand the requirement
We want to print only positive numbers inside Some, ignoring None and non-positive.
Step 2: Analyze each option
for val in list {
match val {
Some(n) if n > 0 => println!("Positive: {}", n),
_ => (),
}
} matches Some(n) with guard n > 0 and prints; others ignore with _ => (). for val in list {
match val {
Some(n) => println!("Positive: {}", n),
None => println!("No value"),
}
} prints all Some values regardless of positivity. for val in list {
if let Some(n) = val {
if n > 0 {
println!("Positive: {}", n);
}
}
} uses if let but is not a match expression. for val in list {
match val {
Some(n) if n <= 0 => println!("Positive: {}", n),
_ => (),
}
} prints non-positive numbers, opposite of requirement.
Final Answer:
for val in list {
match val {
Some(n) if n > 0 => println!("Positive: {}", n),
_ => (),
}
} -> Option A
Quick Check:
Match with guard filters positive Some values [OK]
Hint: Use if guards in match to filter values [OK]
Common Mistakes:
Printing all Some values without checking positivity
Using if let instead of match when asked
Reversing guard condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.