0
0
Rustprogramming~20 mins

Using unwrap and expect in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Unwrap & Expect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
}
A42
BSome(42)
CNone
Dpanic at unwrap on None
Attempts:
2 left
💡 Hint
unwrap() extracts the value inside Some or panics if None.
Predict Output
intermediate
2: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!"));
}
Apanic with message: Value was None!
Bprints None
Cprints 0
DValue was None!
Attempts:
2 left
💡 Hint
expect() panics with the given message if the Option is None.
🔧 Debug
advanced
2: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());
}
Acompiles but prints 0
Bprints error occurred
Cpanic with message: called `Result::unwrap()` on an Err value: "error occurred"
Dsyntax error
Attempts:
2 left
💡 Hint
unwrap() on Err causes a panic with the error inside.
Predict Output
advanced
2: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");
}
Apanic with message: called `Option::unwrap()` on a None value
Bcompiles but does nothing
Cprints None
Dpanic with message: Custom panic message
Attempts:
2 left
💡 Hint
expect() panics with the custom message if Option is None.
🧠 Conceptual
expert
2: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?
Aunwrap() is slower than expect()
Bexpect() allows providing a clear panic message, making debugging easier
Cunwrap() cannot be used on Result types
Dexpect() automatically recovers from errors
Attempts:
2 left
💡 Hint
Think about error messages and debugging.