Challenge - 5 Problems
Rust Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"),
}
}Attempts:
2 left
💡 Hint
Look at which patterns match the value 3.
✗ Incorrect
The value 3 matches the pattern '3 | 4', so the program prints 'Three or Four'.
❓ Predict Output
intermediate2: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"),
}
}Attempts:
2 left
💡 Hint
Check which pattern matches 5 and whether the guard applies.
✗ Incorrect
The value 5 matches the pattern '4 | 5 | 6' without any guard, so it prints 'Medium number'. The guard applies only to '1 | 2 | 3'.
🔧 Debug
advanced2: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"),
}
}Attempts:
2 left
💡 Hint
Look carefully at the pattern list and the trailing '|'.
✗ Incorrect
The trailing '|' after '2' is invalid syntax. The compiler expects a pattern after '|', but finds none.
❓ Predict Output
advanced2: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"),
}
}Attempts:
2 left
💡 Hint
Check which pattern matches the tuple (2, 3).
✗ Incorrect
The tuple (2, 3) matches the pattern '(2, 3) | (3, 2)', so it prints 'Numbers are 2 and 3 in any order'.
❓ Predict Output
expert2: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);
}Attempts:
2 left
💡 Hint
Check which pattern matches the character 'x'.
✗ Incorrect
The character 'x' matches the pattern 'x | y | z', so `result` is set to "special consonant".