0
0
Rustprogramming~20 mins

Matching multiple patterns in Rust - Practice Problems & Coding Challenges

Choose your learning style9 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'.