0
0
Rustprogramming~10 mins

Using unwrap and expect in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to unwrap the Option value safely.

Rust
let some_number = Some(10);
let number = some_number.[1]();
println!("Number: {}", number);
Drag options to blanks, or click blank then click option'
Aunwrap_or_else
Bexpect
Cunwrap_or
Dunwrap
Attempts:
3 left
💡 Hint
Common Mistakes
Using expect without a message causes a compile error.
Using unwrap_or requires a default value argument.
2fill in blank
medium

Complete the code to unwrap the Option with a custom panic message.

Rust
let some_text = Some("hello");
let text = some_text.[1]("No text found!");
println!("Text: {}", text);
Drag options to blanks, or click blank then click option'
Aexpect
Bunwrap_or
Cunwrap
Dunwrap_or_else
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap does not allow a custom message.
Using unwrap_or requires a default value, not a message.
3fill in blank
hard

Fix the error in unwrapping the Result value with expect.

Rust
let result: Result<i32, &str> = Ok(5);
let value = result.[1]("Failed to get value");
println!("Value: {}", value);
Drag options to blanks, or click blank then click option'
Aunwrap_or
Bexpect
Cunwrap_err
Dunwrap
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap_err unwraps the error, not the success value.
Using unwrap_or requires a default value, not a message.
4fill in blank
hard

Fill both blanks to unwrap an Option and provide a custom panic message.

Rust
let maybe_value = Some(42);
let value = maybe_value.[1]("[2] not found");
println!("Value is {}", value);
Drag options to blanks, or click blank then click option'
Aexpect
Bunwrap
COption
DResult
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap instead of expect for custom messages.
Using Result instead of Option in the message.
5fill in blank
hard

Fill all three blanks to unwrap a Result and handle the error with a custom message.

Rust
let res: Result<&str, &str> = Err("error");
let val = res.[1]("[2] failed");
println!("Value: {}", [3]);
Drag options to blanks, or click blank then click option'
Aunwrap_or
Bexpect
Cval
Dunwrap
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap_or instead of expect for custom messages.
Printing the wrong variable name.