Challenge - 5 Problems
Rust Match with Enums Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of matching enum variants with data
What is the output of this Rust program?
Rust
enum Status { Success(u8), Error(String), Pending, } fn main() { let s = Status::Success(200); match s { Status::Success(code) => println!("Code: {}", code), Status::Error(msg) => println!("Error: {}", msg), Status::Pending => println!("Pending..."), } }
Attempts:
2 left
💡 Hint
Look at how the match extracts the inner value from the Success variant.
✗ Incorrect
The match arm Status::Success(code) extracts the inner u8 value 200 and prints it with the label 'Code:'.
❓ Predict Output
intermediateMatching nested enums with multiple patterns
What will this Rust program print?
Rust
enum Direction { Left, Right, } enum Command { Move(Direction), Stop, } fn main() { let cmd = Command::Move(Direction::Right); match cmd { Command::Move(Direction::Left) => println!("Moving left"), Command::Move(Direction::Right) => println!("Moving right"), Command::Stop => println!("Stopped"), } }
Attempts:
2 left
💡 Hint
Check which Direction variant is inside the Command::Move variant.
✗ Incorrect
The variable cmd is Command::Move(Direction::Right), so the match arm prints 'Moving right'.
🔧 Debug
advancedIdentify the error in matching enum with missing variant
What error will this Rust code produce when compiled?
Rust
enum Light { Red, Yellow, Green, } fn main() { let light = Light::Yellow; match light { Light::Red => println!("Stop"), Light::Green => println!("Go"), } }
Attempts:
2 left
💡 Hint
Rust requires all enum variants to be matched or covered by a wildcard.
✗ Incorrect
The match does not cover Light::Yellow variant, so the compiler reports a non-exhaustive patterns error.
📝 Syntax
advancedCorrect syntax for matching enum with data
Which option correctly matches and extracts data from this enum variant?
Rust
enum Message { Text(String), Quit, } fn main() { let msg = Message::Text(String::from("Hello")); match msg { // Fill in the correct pattern here } }
Attempts:
2 left
💡 Hint
The pattern must bind the inner String to a variable and use it in println! macro.
✗ Incorrect
Option B correctly matches Message::Text and binds the inner String to 'text', then prints it with a label.
🚀 Application
expertDetermine the final value after matching and updating enum
Given this Rust code, what is the final value of 'status' after the match block?
Rust
enum Status { Active(i32), Inactive, } fn main() { let mut status = Status::Active(10); match &mut status { Status::Active(ref mut val) if *val > 5 => *val += 5, Status::Active(_) => (), Status::Inactive => status = Status::Active(1), } // What is the value of status here? }
Attempts:
2 left
💡 Hint
The match uses a guard to check if the value is greater than 5 and updates it.
✗ Incorrect
The initial value is Active(10), which matches the first arm with guard val > 5, so val is increased by 5 to 15.
