Challenge - 5 Problems
Generic Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of generic enum pattern matching
What is the output of this Rust program using a generic enum and pattern matching?
Rust
enum OptionValue<T> { Some(T), None, } fn main() { let x: OptionValue<i32> = OptionValue::Some(10); match x { OptionValue::Some(val) if val > 5 => println!("Greater than 5: {}", val), OptionValue::Some(val) => println!("Value: {}", val), OptionValue::None => println!("No value"), } }
Attempts:
2 left
💡 Hint
Look at the pattern guard `if val > 5` and the value inside Some.
✗ Incorrect
The enum holds Some(10). The match arm with guard `if val > 5` matches because 10 > 5, so it prints "Greater than 5: 10".
❓ Predict Output
intermediate2:00remaining
Result of generic enum with different types
What will this Rust code print when run?
Rust
enum Container<T> { Item(T), Empty, } fn main() { let a: Container<&str> = Container::Item("hello"); let b: Container<i32> = Container::Empty; match a { Container::Item(s) => println!("String: {}", s), Container::Empty => println!("Empty container"), } match b { Container::Item(n) => println!("Number: {}", n), Container::Empty => println!("Empty container"), } }
Attempts:
2 left
💡 Hint
Check the variant used for each variable and the match arms.
✗ Incorrect
Variable `a` holds `Item("hello")`, so it prints "String: hello". Variable `b` holds `Empty`, so it prints "Empty container".
🔧 Debug
advanced2:00remaining
Why does this generic enum code fail to compile?
This Rust code tries to implement a method for a generic enum but fails. What is the cause of the compilation error?
Rust
enum Wrapper<T> { Value(T), None, } impl<T> Wrapper<T> { fn unwrap(self) -> T { match self { Wrapper::Value(val) => val, Wrapper::None => panic!("No value"), } } } fn main() { let w: Wrapper<i32> = Wrapper::None; println!("{}", w.unwrap()); }
Attempts:
2 left
💡 Hint
Check if the method unwrap handles all variants and what happens at runtime.
✗ Incorrect
The code compiles fine. The unwrap method returns the value for Value variant or panics for None variant. The program panics at runtime when unwrap is called on None.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this generic enum definition
Which option correctly identifies the syntax error in this Rust code?
Rust
enum Result<T, E> { Ok(T), Err(E) fn main() { let r: Result<i32, &str> = Result::Ok(5); }
Attempts:
2 left
💡 Hint
Check the braces that open and close the enum block.
✗ Incorrect
The enum block is missing the closing brace '}' after the variants, causing a syntax error.
🚀 Application
expert2:00remaining
How many items are in this generic enum vector?
Given this Rust code, what number does the program print?
Rust
enum Data<T> { Single(T), Multiple(Vec<T>), } fn main() { let items = vec![ Data::Single(1), Data::Multiple(vec![2, 3]), Data::Single(4), ]; let count = items.iter().fold(0, |acc, item| { match item { Data::Single(_) => acc + 1, Data::Multiple(v) => acc + v.len(), } }); println!("Count: {}", count); }
Attempts:
2 left
💡 Hint
Count single items as 1 and multiple items by their vector length.
✗ Incorrect
The vector has 3 elements: Single(1) counts as 1, Multiple(vec![2,3]) counts as 2, Single(4) counts as 1. Total is 1 + 2 + 1 = 4.