Challenge - 5 Problems
Rust Enums Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust enum with data?
Consider the following Rust code using an enum with data. What will be printed when running this program?
Rust
enum Message { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), } fn main() { let msg = Message::Move { x: 10, y: 20 }; match msg { Message::Quit => println!("Quit"), Message::Move { x, y } => println!("Move to ({}, {})", x, y), Message::Write(text) => println!("Write: {}", text), Message::ChangeColor(r, g, b) => println!("Change color to ({}, {}, {})", r, g, b), } }
Attempts:
2 left
💡 Hint
Look at the variant used and how the match arms print the values.
✗ Incorrect
The enum variant used is Move with x=10 and y=20. The match arm for Move prints these values in the format "Move to (x, y)".
❓ Predict Output
intermediateWhat does this Rust enum with tuple data print?
Given this Rust code, what is the output when main runs?
Rust
enum Shape { Circle(f64), Rectangle(f64, f64), } fn main() { let shape = Shape::Rectangle(3.0, 4.0); match shape { Shape::Circle(radius) => println!("Circle with radius {}", radius), Shape::Rectangle(width, height) => println!("Rectangle {} x {}", width, height), } }
Attempts:
2 left
💡 Hint
Check which variant is created and how the values are printed.
✗ Incorrect
The Shape::Rectangle variant is created with width=3.0 and height=4.0, so the output is "Rectangle 3 x 4".
🔧 Debug
advancedWhy does this Rust enum code fail to compile?
This Rust code tries to use an enum with data but does not compile. What is the cause of the error?
Rust
enum Command { Start(u32), Stop, } fn main() { let cmd = Command::Start; match cmd { Command::Start(time) => println!("Start at {}", time), Command::Stop => println!("Stop"), } }
Attempts:
2 left
💡 Hint
Look at how the Start variant is created and what data it expects.
✗ Incorrect
The Start variant requires a u32 value, but Command::Start is used without providing one, causing a compilation error.
📝 Syntax
advancedWhich option correctly defines an enum with named fields in Rust?
Select the correct Rust enum definition that uses named fields for one variant.
Attempts:
2 left
💡 Hint
Named fields require curly braces with field names and types.
✗ Incorrect
Option A correctly uses named fields with types inside curly braces for Click variant. Others have syntax errors or missing types.
🚀 Application
expertHow many items are in the vector after filtering enum variants with data?
Given this Rust code, how many elements remain in the vector after filtering only the Message::Write variants?
Rust
enum Message { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), } fn main() { let messages = vec![ Message::Quit, Message::Write(String::from("Hello")), Message::Move { x: 1, y: 2 }, Message::Write(String::from("World")), Message::ChangeColor(255, 0, 0), ]; let writes: Vec<&Message> = messages.iter().filter(|m| { match m { Message::Write(_) => true, _ => false, } }).collect(); println!("Number of Write messages: {}", writes.len()); }
Attempts:
2 left
💡 Hint
Count how many Message::Write variants are in the vector.
✗ Incorrect
There are exactly two Message::Write variants in the vector, so the filtered vector length is 2.
