Challenge - 5 Problems
Rust Unwrap & Expect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of unwrap on Some value
What is the output of this Rust code?
Rust
fn main() {
let value = Some(42);
println!("{}", value.unwrap());
}Attempts:
2 left
💡 Hint
unwrap() extracts the value inside Some or panics if None.
✗ Incorrect
unwrap() returns the inner value if the Option is Some. Here, value is Some(42), so unwrap() returns 42.
❓ Predict Output
intermediate2:00remaining
Using expect with a custom panic message
What will this Rust program print or do?
Rust
fn main() {
let value: Option<i32> = None;
println!("{}", value.expect("Value was None!"));
}Attempts:
2 left
💡 Hint
expect() panics with the given message if the Option is None.
✗ Incorrect
Since value is None, calling expect() causes a panic with the message 'Value was None!'.
🔧 Debug
advanced2:00remaining
Identify the error when unwrapping a Result
What error does this Rust code produce when run?
Rust
fn main() {
let result: Result<i32, &str> = Err("error occurred");
println!("{}", result.unwrap());
}Attempts:
2 left
💡 Hint
unwrap() on Err causes a panic with the error inside.
✗ Incorrect
Calling unwrap() on an Err variant causes a panic with the error message inside Err.
❓ Predict Output
advanced2:00remaining
Difference between unwrap and expect output
What is the output of this Rust code?
Rust
fn main() {
let opt: Option<&str> = None;
opt.expect("Custom panic message");
}Attempts:
2 left
💡 Hint
expect() panics with the custom message if Option is None.
✗ Incorrect
expect() panics with the provided message when called on None, unlike unwrap() which uses a default message.
🧠 Conceptual
expert2:00remaining
Why prefer expect over unwrap in production code?
Which is the best reason to prefer using expect() instead of unwrap() in Rust production code?
Attempts:
2 left
💡 Hint
Think about error messages and debugging.
✗ Incorrect
Using expect() lets you add a custom message that helps identify the cause of a panic, which is very useful in production debugging.