A match guard is an additional condition specified after a pattern in a match arm using if. It lets you add extra checks to decide if that arm should be chosen.
Click to reveal answer
beginner
How do you write a match guard in Rust?
You write a match guard by adding if condition after the pattern in a match arm. For example: Some(x) if x > 5 => ...
Click to reveal answer
intermediate
Why use match guards instead of just matching patterns?
Match guards let you check extra conditions that patterns alone can't express, like comparing values or calling functions, making your matching more precise.
Click to reveal answer
intermediate
What happens if multiple match arms match but only one has a true guard?
Rust chooses the first arm where the pattern matches and the guard condition is true. If the guard is false, Rust continues checking the next arms.
Click to reveal answer
intermediate
Can match guards use variables bound in the pattern?
Yes! Variables bound in the pattern can be used in the guard condition to make decisions based on their values.
Click to reveal answer
What keyword introduces a match guard in Rust?
Aif
Belse
Cmatch
Dguard
✗ Incorrect
Match guards use the if keyword after a pattern to add extra conditions.
Which arm will be chosen in this code?
match x {
Some(n) if n > 10 => "big",
Some(_) => "small",
None => "none",
}
AThe first arm if n > 10
BThe third arm if x is None
CBoth A and C depending on x
DThe second arm always
✗ Incorrect
If x is Some(n) and n > 10, the first arm matches. If x is Some(n) but n ≤ 10, the second arm matches. If x is None, the third arm matches.
Can a match guard call functions?
AYes, any function can be called in a guard
BNo, only simple comparisons are allowed
COnly functions without parameters
DOnly functions returning booleans
✗ Incorrect
Match guards can call any function as long as it returns a boolean value.
What happens if a guard condition is false?
ARust restarts the match
BRust panics
CRust chooses that arm anyway
DRust skips that arm and checks the next
✗ Incorrect
If the guard condition is false, Rust ignores that arm and continues checking the next arms.
Can match guards be used with all pattern types?
ANo, only with enum variants
BYes, with any pattern
COnly with tuple patterns
DOnly with literals
✗ Incorrect
Match guards can be used with any pattern to add extra conditions.
Explain what a match guard is and how it changes pattern matching behavior in Rust.
Think about how you can add an extra check after matching a pattern.
You got /4 concepts.
Describe a situation where using a match guard is better than just matching a pattern.
Consider when you want to match a pattern only if a value meets a certain condition.
You got /3 concepts.
Practice
(1/5)
1.
What is the purpose of a match guard in Rust?
easy
A. To add an extra condition to a pattern in a match statement
B. To replace the entire match statement with an if statement
C. To declare a new variable inside a match arm
D. To loop over all patterns automatically
Solution
Step 1: Understand match guards
Match guards add extra conditions to patterns in a match, allowing more precise checks.
Step 2: Compare options
Only To add an extra condition to a pattern in a match statement correctly describes adding extra conditions to patterns.
Final Answer:
To add an extra condition to a pattern in a match statement -> Option A
Quick Check:
Match guard = extra condition [OK]
Hint: Match guards add conditions after patterns using if [OK]
Common Mistakes:
Thinking match guards replace match statements
Confusing match guards with variable declarations
Assuming match guards loop over patterns
2.
Which of the following is the correct syntax for a match guard in Rust?
match x {
5 if x > 3 => println!("Greater than 3"),
_ => println!("Other")
}
easy
A. match x { if 5 > 3 => println!("Greater than 3"), _ => println!("Other") }
B. match x { 5 => if x > 3 { println!("Greater than 3") }, _ => println!("Other") }
C. match x { 5 if x > 3 => println!("Greater than 3"), _ => println!("Other") }
D. match x { 5 => println!("Greater than 3") if x > 3, _ => println!("Other") }
Solution
Step 1: Identify correct match guard syntax
Match guards use pattern if condition => expression syntax.
Step 2: Check options
match x { 5 if x > 3 => println!("Greater than 3"), _ => println!("Other") } correctly places if x > 3 after the pattern 5.
Final Answer:
match x { 5 if x > 3 => println!("Greater than 3"), _ => println!("Other") } -> Option C
Quick Check:
Pattern if condition => action [OK]
Hint: Use 'pattern if condition => action' for match guards [OK]
Common Mistakes:
Placing if before the pattern
Using if inside the match arm block
Putting if after the expression
3.
What will be the output of the following Rust code?
let num = 7;
match num {
n if n < 5 => println!("Less than 5"),
n if n <= 10 => println!("Between 5 and 10"),
_ => println!("Greater than 10"),
}
medium
A. Between 5 and 10
B. No output (compile error)
C. Greater than 10
D. Less than 5
Solution
Step 1: Evaluate match arms in order
num is 7, first guard checks if 7 < 5 (false), so skip.
Step 2: Check second guard
7 <= 10 is true, so this arm runs and prints "Between 5 and 10".
Final Answer:
Between 5 and 10 -> Option A
Quick Check:
7 ≤ 10 matches second guard [OK]
Hint: Match arms checked top to bottom; first true guard runs [OK]
Common Mistakes:
Assuming first guard matches 7
Thinking all guards run
Confusing output with compile error
4.
Identify the error in this Rust code using match guards:
let x = 10;
match x {
n if n > 5 => println!("Greater than 5"),
n if n > 8 => println!("Greater than 8"),
_ => println!("Other"),
}
medium
A. Syntax error: match guards cannot use variables
B. The wildcard pattern must come before guarded patterns
C. Missing semicolon after println! macros
D. The second guard is unreachable because the first guard covers all cases greater than 5
Solution
Step 1: Analyze guard order and coverage
The first guard matches all n > 5, so it includes all n > 8 as well.
Step 2: Identify unreachable code
The second guard is never reached because the first guard catches those values first.
Final Answer:
The second guard is unreachable because the first guard covers all cases greater than 5 -> Option D
Quick Check:
First guard shadows second guard [OK]
Hint: Order guards from most specific to least specific [OK]
Common Mistakes:
Ignoring unreachable code warnings
Thinking guards can overlap safely
Misunderstanding guard evaluation order
5.
Given a list of integers, write a Rust match expression with match guards to print:
"Small" if the number is less than 10
"Medium" if the number is between 10 and 20 (inclusive)
"Large" if the number is greater than 20
Which of the following match arms correctly implements this logic for a variable n?
hard
A. match n {
x if x <= 10 => println!("Small"),
x if x > 10 && x < 20 => println!("Medium"),
_ => println!("Large"),
}
B. match n {
x if x < 10 => println!("Small"),
x if x >= 10 && x <= 20 => println!("Medium"),
_ => println!("Large"),
}
C. match n {
x if x < 10 => println!("Small"),
x if x > 20 => println!("Medium"),
_ => println!("Large"),
}
D. match n {
x if x < 10 => println!("Small"),
x if x > 10 && x < 20 => println!("Medium"),
_ => println!("Large"),
}
Solution
Step 1: Check conditions for each size category
"Small" is for n < 10, "Medium" for 10 ≤ n ≤ 20, "Large" for n > 20.
Step 2: Verify each match arm
match n {
x if x < 10 => println!("Small"),
x if x >= 10 && x <= 20 => println!("Medium"),
_ => println!("Large"),
} correctly uses x < 10, x >= 10 && x <= 20, and wildcard for others.
Final Answer:
match n {
x if x < 10 => println!("Small"),
x if x >= 10 && x <= 20 => println!("Medium"),
_ => println!("Large"),
} -> Option B
Quick Check:
Guards match exact ranges [OK]
Hint: Use inclusive ranges with && for medium category [OK]
Common Mistakes:
Using wrong comparison operators
Overlapping ranges causing unreachable arms
Missing inclusive bounds for medium range
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.