Bird
Raised Fist0
Rustprogramming~20 mins

Matching multiple patterns in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using multiple pattern matching?
Consider the following Rust code that uses a match statement with multiple patterns combined using the | operator. What will it print?
Rust
fn main() {
    let x = 3;
    match x {
        1 | 2 => println!("One or Two"),
        3 | 4 => println!("Three or Four"),
        _ => println!("Other"),
    }
}
AOne or Two
BOther
CThree or Four
DCompilation error
Attempts:
2 left
💡 Hint
Look at which patterns match the value 3.
❓ Predict Output
intermediate
2:00remaining
What does this Rust code print when matching multiple patterns with guards?
Analyze the following Rust code that uses multiple patterns with a guard condition. What will it print?
Rust
fn main() {
    let x = 5;
    match x {
        1 | 2 | 3 if x % 2 == 0 => println!("Small even number"),
        4 | 5 | 6 => println!("Medium number"),
        _ => println!("Other"),
    }
}
ACompilation error
BMedium number
COther
DSmall even number
Attempts:
2 left
💡 Hint
Check which pattern matches 5 and whether the guard applies.
🔧 Debug
advanced
2:00remaining
What error does this Rust code produce when matching multiple patterns incorrectly?
This Rust code tries to match multiple patterns but has a syntax mistake. What error will the compiler show?
Rust
fn main() {
    let x = 2;
    match x {
        1 | 2 | => println!("One or Two"),
        _ => println!("Other"),
    }
}
AError: expected pattern, found '|'
BSyntaxError: unexpected token '|'
CError: missing comma between patterns
DNo error, compiles successfully
Attempts:
2 left
💡 Hint
Look carefully at the pattern list and the trailing '|'.
❓ Predict Output
advanced
2:00remaining
What is the output of this Rust code using nested multiple pattern matching?
Examine the Rust code below that uses nested match with multiple patterns. What will it print?
Rust
fn main() {
    let pair = (2, 3);
    match pair {
        (1, 1) | (2, 2) => println!("Both numbers are the same and either 1 or 2"),
        (2, 3) | (3, 2) => println!("Numbers are 2 and 3 in any order"),
        _ => println!("Other pair"),
    }
}
ABoth numbers are the same and either 1 or 2
BCompilation error
COther pair
DNumbers are 2 and 3 in any order
Attempts:
2 left
💡 Hint
Check which pattern matches the tuple (2, 3).
❓ Predict Output
expert
2:00remaining
What is the value of variable `result` after this Rust code runs with multiple pattern matching?
Consider this Rust code that uses match with multiple patterns and returns a value. What is the final value of `result`?
Rust
fn main() {
    let ch = 'x';
    let result = match ch {
        'a' | 'e' | 'i' | 'o' | 'u' => "vowel",
        'x' | 'y' | 'z' => "special consonant",
        _ => "consonant",
    };
    println!("{}", result);
}
A"special consonant"
B"vowel"
C"consonant"
DCompilation error
Attempts:
2 left
💡 Hint
Check which pattern matches the character 'x'.

Practice

(1/5)
1. In Rust, which symbol is used to match multiple patterns in a single match arm?
easy
A. |
B. &
C. +
D. &&

Solution

  1. Step 1: Recall the syntax for matching multiple patterns

    Rust uses the vertical bar | to separate multiple patterns in one match arm.
  2. Step 2: Identify the correct symbol

    The symbol | means "or" in pattern matching, allowing multiple cases to be handled together.
  3. Final Answer:

    | -> Option A
  4. Quick Check:

    Multiple patterns use | in match arms [OK]
Hint: Use | to combine patterns in one match arm [OK]
Common Mistakes:
  • Using & instead of |
  • Confusing + with pattern separator
  • Using && which is logical AND, not pattern OR
2. Which of the following Rust match arms correctly matches the values 1, 2, or 3 using multiple patterns?
easy
A. 1 & 2 & 3 => println!("Matched 1, 2, or 3"),
B. 1 | 2 | 3 => println!("Matched 1, 2, or 3"),
C. 1 + 2 + 3 => println!("Matched 1, 2, or 3"),
D. 1, 2, 3 => println!("Matched 1, 2, or 3"),

Solution

  1. Step 1: Check the syntax for multiple patterns

    Rust uses | to separate multiple patterns in a match arm, so 1 | 2 | 3 is correct.
  2. Step 2: Identify incorrect options

    & is not used for matching multiple patterns, + is arithmetic, and commas are invalid here.
  3. Final Answer:

    1 | 2 | 3 => println!("Matched 1, 2, or 3"), -> Option B
  4. Quick Check:

    Multiple patterns use | separator [OK]
Hint: Separate patterns with | inside match arms [OK]
Common Mistakes:
  • Using & instead of | for multiple patterns
  • Trying to separate patterns with commas
  • Using + which is arithmetic, not pattern matching
3. What will be the output of this Rust code?
let x = 2;
match x {
    1 | 2 | 3 => println!("Small number"),
    4 | 5 | 6 => println!("Medium number"),
    _ => println!("Other number"),
}
medium
A. Compilation error
B. Medium number
C. Small number
D. Other number

Solution

  1. Step 1: Identify which pattern matches x=2

    The first arm matches 1, 2, or 3. Since x is 2, it matches this arm.
  2. Step 2: Determine the output

    The matched arm prints "Small number" to the console.
  3. Final Answer:

    Small number -> Option C
  4. Quick Check:

    2 matches 1|2|3 arm [OK]
Hint: Check which pattern includes the value [OK]
Common Mistakes:
  • Choosing Medium number for 2
  • Thinking _ arm matches first
  • Assuming syntax error due to multiple patterns
4. Find the error in this Rust code snippet that tries to match multiple patterns:
let ch = 'a';
match ch {
    'a' | 'e' | 'i' | 'o' | 'u' => println!("Vowel"),
    'a' | 'b' => println!("Letter a or b"),
    _ => println!("Other"),
}
medium
A. Missing semicolon after println! macros
B. Match expression missing a default arm
C. Incorrect use of | operator for multiple patterns
D. Duplicate pattern 'a' in multiple arms causes unreachable code

Solution

  1. Step 1: Identify duplicate patterns in match arms

    The character 'a' appears in both the first and second arms, causing the second arm's 'a' pattern to be unreachable.
  2. Step 2: Understand Rust's pattern matching rules

    Rust does not allow unreachable patterns; the compiler will warn or error about the duplicate 'a'.
  3. Final Answer:

    Duplicate pattern 'a' in multiple arms causes unreachable code -> Option D
  4. Quick Check:

    Duplicate patterns cause unreachable code errors [OK]
Hint: Avoid repeating same pattern in multiple match arms [OK]
Common Mistakes:
  • Ignoring unreachable pattern warnings
  • Thinking | is invalid for multiple patterns
  • Missing default arm (actually present here)
5. You want to write a Rust function that returns "Weekend" for Saturday or Sunday, "Weekday" for Monday to Friday, and "Invalid" otherwise. Which match arm correctly uses multiple patterns to do this?
fn day_type(day: &str) -> &str {
    match day {
        ? => "Weekend",
        ? => "Weekday",
        _ => "Invalid",
    }
}
hard
A. "Saturday" | "Sunday", "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday"
B. "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
C. "Saturday" & "Sunday", "Monday" & "Tuesday" & "Wednesday" & "Thursday" & "Friday"
D. "Saturday" + "Sunday", "Monday" + "Tuesday" + "Wednesday" + "Thursday" + "Friday"

Solution

  1. Step 1: Use | to match multiple string patterns in one arm

    Rust allows multiple patterns separated by | to group cases like "Saturday" | "Sunday".
  2. Step 2: Check other options for invalid syntax

    Commas separate arms, not patterns; & and + are invalid for pattern matching.
  3. Final Answer:

    "Saturday" | "Sunday", "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" -> Option A
  4. Quick Check:

    Use | to combine multiple string patterns [OK]
Hint: Use | to combine multiple string patterns in match arms [OK]
Common Mistakes:
  • Using commas instead of | inside match arms
  • Using & or + which are invalid for patterns
  • Forgetting to cover all weekdays or weekend days
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