Challenge - 5 Problems
Rust Match 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 match?
Look at this Rust program. What will it print when run?
Rust
fn main() {
let number = 3;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Other"),
}
}Attempts:
2 left
๐ก Hint
Check which pattern matches the value 3 in the match expression.
โ Incorrect
The variable 'number' is 3, so the match arm '3 => println!("Three")' runs, printing 'Three'.
โ Predict Output
intermediate2:00remaining
What does this Rust match expression return?
What value does this Rust code assign to 'result'?
Rust
fn main() {
let x = 5;
let result = match x {
1..=4 => "small",
5..=10 => "medium",
_ => "large",
};
println!("{}", result);
}Attempts:
2 left
๐ก Hint
Look at the range patterns and see where 5 fits.
โ Incorrect
The value 5 matches the range 5..=10, so 'medium' is assigned to 'result'.
โ Predict Output
advanced2:00remaining
What is the output of this nested match in Rust?
Analyze the nested match statements and find the output.
Rust
fn main() {
let a = Some(2);
let b = 3;
match a {
Some(x) => match b {
3 => println!("x is {} and b is three", x),
_ => println!("x is {} and b is not three", x),
},
None => println!("No value for a"),
}
}Attempts:
2 left
๐ก Hint
Check the values inside the nested match carefully.
โ Incorrect
Variable 'a' is Some(2), so first match arm runs. Then 'b' is 3, so inner match prints 'x is 2 and b is three'.
โ Predict Output
advanced2:00remaining
What error does this Rust code produce?
What error will this Rust code cause when compiled?
Rust
fn main() {
let num = 7;
match num {
1 => println!("One"),
2 => println!("Two"),
_ => (),
}
}Attempts:
2 left
๐ก Hint
Think about what happens if 'num' is 7 and no match arm covers it.
โ Incorrect
The match is not exhaustive because it only covers 1 and 2, but 'num' is 7. Rust requires all cases covered or a wildcard '_'.
๐ง Conceptual
expert2:00remaining
How many arms does this Rust match expression have?
Count the number of arms in this Rust match expression.
Rust
fn main() {
let val = 10;
match val {
1 | 2 | 3 => println!("One, two or three"),
4..=10 => println!("Between four and ten"),
_ => println!("Something else"),
}
}Attempts:
2 left
๐ก Hint
Count each separate match arm, not the number of values in patterns.
โ Incorrect
There are three arms: one for 1|2|3, one for 4..=10, and one wildcard '_'.