Bird
Raised Fist0
Rustprogramming~20 mins

Match guards in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Match Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of match with guard on integer
What is the output of this Rust code snippet?
Rust
fn main() {
    let x = 7;
    match x {
        n if n % 2 == 0 => println!("Even"),
        n if n % 2 == 1 => println!("Odd"),
        _ => println!("Unknown"),
    }
}
AOdd
BEven
CUnknown
DCompilation error
Attempts:
2 left
💡 Hint
Check the value of x and the conditions in the match guards.
❓ Predict Output
intermediate
2:00remaining
Match guard with multiple conditions
What will this Rust program print?
Rust
fn main() {
    let c = 'g';
    match c {
        ch if ch.is_ascii_lowercase() && ch < 'm' => println!("First half lowercase"),
        ch if ch.is_ascii_lowercase() => println!("Second half lowercase"),
        _ => println!("Not lowercase"),
    }
}
AFirst half lowercase
BSecond half lowercase
CNot lowercase
DRuntime panic
Attempts:
2 left
💡 Hint
Check the character 'g' against the conditions in order.
❓ Predict Output
advanced
2:00remaining
Match guard with tuple and shadowing
What is the output of this Rust code?
Rust
fn main() {
    let pair = (3, -2);
    match pair {
        (x, y) if x == y => println!("Equal"),
        (x, y) if x + y == 0 => println!("Sum zero"),
        _ => println!("No match"),
    }
}
AEqual
BCompilation error
CNo match
DSum zero
Attempts:
2 left
💡 Hint
Check the values of x and y and the guards in order.
❓ Predict Output
advanced
2:00remaining
Match guard with range and variable binding
What will this Rust program print?
Rust
fn main() {
    let num = 15;
    match num {
        n @ 1..=10 if n % 2 == 0 => println!("Even in 1 to 10"),
        n @ 11..=20 if n % 3 == 0 => println!("Multiple of 3 in 11 to 20"),
        _ => println!("Other"),
    }
}
AEven in 1 to 10
BOther
CCompilation error
DMultiple of 3 in 11 to 20
Attempts:
2 left
💡 Hint
Check which range 15 falls into and if it is divisible by 3.
❓ Predict Output
expert
3:00remaining
Match guard with enum and complex condition
Given this Rust code, what is the output?
Rust
enum Status {
    Active(i32),
    Inactive(i32),
}

fn main() {
    let s = Status::Active(10);
    match s {
        Status::Active(n) if n > 5 && n < 15 => println!("Active and moderate"),
        Status::Active(n) if n >= 15 => println!("Active and high"),
        Status::Inactive(_) => println!("Inactive"),
        _ => println!("Unknown"),
    }
}
AActive and high
BActive and moderate
CInactive
DUnknown
Attempts:
2 left
💡 Hint
Check the enum variant and the value inside it against the guards.

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

  1. Step 1: Understand match guards

    Match guards add extra conditions to patterns in a match, allowing more precise checks.
  2. Step 2: Compare options

    Only To add an extra condition to a pattern in a match statement correctly describes adding extra conditions to patterns.
  3. Final Answer:

    To add an extra condition to a pattern in a match statement -> Option A
  4. 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

  1. Step 1: Identify correct match guard syntax

    Match guards use pattern if condition => expression syntax.
  2. 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.
  3. Final Answer:

    match x { 5 if x > 3 => println!("Greater than 3"), _ => println!("Other") } -> Option C
  4. 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

  1. Step 1: Evaluate match arms in order

    num is 7, first guard checks if 7 < 5 (false), so skip.
  2. Step 2: Check second guard

    7 <= 10 is true, so this arm runs and prints "Between 5 and 10".
  3. Final Answer:

    Between 5 and 10 -> Option A
  4. 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

  1. Step 1: Analyze guard order and coverage

    The first guard matches all n > 5, so it includes all n > 8 as well.
  2. Step 2: Identify unreachable code

    The second guard is never reached because the first guard catches those values first.
  3. Final Answer:

    The second guard is unreachable because the first guard covers all cases greater than 5 -> Option D
  4. 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

  1. Step 1: Check conditions for each size category

    "Small" is for n < 10, "Medium" for 10 ≤ n ≤ 20, "Large" for n > 20.
  2. 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.
  3. Final Answer:

    match n { x if x < 10 => println!("Small"), x if x >= 10 && x <= 20 => println!("Medium"), _ => println!("Large"), } -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard