Challenge - 5 Problems
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of matching enum variants
What is the output of this Rust program?
Rust
enum Direction { North, East, South, West, } fn main() { let dir = Direction::South; match dir { Direction::North => println!("Going North"), Direction::East => println!("Going East"), Direction::South => println!("Going South"), Direction::West => println!("Going West"), } }
Attempts:
2 left
💡 Hint
Look at which variant is assigned to dir and which arm matches it.
✗ Incorrect
The variable dir is set to Direction::South. The match statement prints the string corresponding to the matched variant. So it prints "Going South".
❓ Predict Output
intermediateOutput with enum variant holding data
What will this Rust program print?
Rust
enum Message { Quit, Move { x: i32, y: i32 }, Write(String), } fn main() { let msg = Message::Move { x: 10, y: 20 }; match msg { Message::Quit => println!("Quit message"), Message::Move { x, y } => println!("Move to ({}, {})", x, y), Message::Write(text) => println!("Write message: {}", text), } }
Attempts:
2 left
💡 Hint
Check the variant used and how the data inside it is accessed.
✗ Incorrect
The variable msg is Message::Move with x=10 and y=20. The match arm extracts x and y and prints them, so output is "Move to (10, 20)".
❓ Predict Output
advancedOutput of enum with tuple variants
What is the output of this Rust code?
Rust
enum Shape { Circle(f64), Rectangle(f64, f64), } fn area(shape: Shape) -> f64 { match shape { Shape::Circle(radius) => 3.14 * radius * radius, Shape::Rectangle(width, height) => width * height, } } fn main() { let c = Shape::Circle(2.0); let r = Shape::Rectangle(3.0, 4.0); println!("Circle area: {:.2}", area(c)); println!("Rectangle area: {:.2}", area(r)); }
Attempts:
2 left
💡 Hint
Calculate area of circle with radius 2 and rectangle 3x4.
✗ Incorrect
Circle area = π * 2^2 = 3.14 * 4 = 12.56. Rectangle area = 3 * 4 = 12.00. So output matches option D.
❓ Predict Output
advancedOutput when matching nested enum variants
What does this Rust program print?
Rust
enum Status { Success, Error(String), } enum Response { Ok(Status), Fail(Status), } fn main() { let res = Response::Fail(Status::Error(String::from("Timeout"))); match res { Response::Ok(Status::Success) => println!("Operation succeeded"), Response::Ok(Status::Error(msg)) => println!("Operation failed: {}", msg), Response::Fail(Status::Success) => println!("Failure but success status"), Response::Fail(Status::Error(msg)) => println!("Failure with error: {}", msg), } }
Attempts:
2 left
💡 Hint
Look at the outer and inner enum variants used in res.
✗ Incorrect
res is Response::Fail with Status::Error("Timeout"). The matching arm prints "Failure with error: Timeout".
🧠 Conceptual
expertNumber of variants in this enum
How many distinct variants does this Rust enum have?
Rust
enum Event { Click, KeyPress(char), Resize { width: u32, height: u32 }, Hover, Scroll(i32), }
Attempts:
2 left
💡 Hint
Count each variant name regardless of data it holds.
✗ Incorrect
The enum Event has 5 variants: Click, KeyPress, Resize, Hover, and Scroll.
