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 match expression?
Consider the following Rust code snippet. What will it print when run?
Rust
let number = 3; match number { 1 => println!("One"), 2 | 3 | 5 | 7 | 11 => println!("This is a prime number"), 13..=19 => println!("A teen number"), _ => println!("Something else"), }
Attempts:
2 left
๐ก Hint
Look at the pattern that matches the value 3.
โ Incorrect
The match arm '2 | 3 | 5 | 7 | 11' matches the value 3, so it prints 'This is a prime number'.
โ Predict Output
intermediate2:00remaining
What does this Rust match with tuple print?
Given this Rust code, what is the output?
Rust
let pair = (0, -2); match pair { (0, y) => println!("First is zero and second is {}", y), (x, 0) => println!("First is {} and second is zero", x), _ => println!("No zeros"), }
Attempts:
2 left
๐ก Hint
Check which pattern matches the tuple (0, -2).
โ Incorrect
The pattern (0, y) matches because the first element is 0, so it prints 'First is zero and second is -2'.
๐ง Debug
advanced2:00remaining
What error does this Rust match code produce?
This Rust code snippet tries to match a value but has a problem. What error will the compiler show?
Rust
let x = 5; match x { 1 => println!("One"), 2 => println!("Two"), 3 => println!("Three"), }
Attempts:
2 left
๐ก Hint
Rust requires match expressions to cover all possible values.
โ Incorrect
The match does not cover all possible values of x, so the compiler reports a non-exhaustive patterns error.
๐ง Conceptual
advanced2:00remaining
Which match arm will be chosen for this Rust code?
What will the following Rust code print?
Rust
let value = Some(10); match value { Some(x) if x > 5 => println!("Greater than five: {}", x), Some(x) => println!("Less or equal to five: {}", x), None => println!("No value"), }
Attempts:
2 left
๐ก Hint
Check the if guard condition in the first arm.
โ Incorrect
The value is Some(10), and 10 > 5, so the first arm with the if guard matches and prints 'Greater than five: 10'.
โ Predict Output
expert3:00remaining
What is the output of this nested match in Rust?
Analyze this Rust code and determine what it prints.
Rust
enum Color { Red, Green, Blue, } let color = Color::Green; match color { Color::Red => println!("Red"), Color::Green => match color { Color::Green => println!("Green inside"), _ => println!("Other inside"), }, Color::Blue => println!("Blue"), }
Attempts:
2 left
๐ก Hint
Look at the nested match inside the Color::Green arm.
โ Incorrect
The outer match matches Color::Green, then the inner match also matches Color::Green, so it prints 'Green inside'.