Bird
Raised Fist0
Rustprogramming~10 mins

Using unwrap and expect in Rust - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the unwrap() method do when called on an Option in Rust?
easy
A. Always returns Some without checking the value.
B. Returns None if the value is missing without panicking.
C. Returns the value inside if Some, or panics if None.
D. Converts the Option into a Result type.

Solution

  1. Step 1: Understand unwrap behavior on Option

    The unwrap() method extracts the value inside an Option if it is Some.
  2. Step 2: What happens if Option is None

    If the Option is None, unwrap() causes the program to panic and stop.
  3. Final Answer:

    Returns the value inside if Some, or panics if None. -> Option C
  4. Quick Check:

    unwrap() panics on None = C [OK]
Hint: unwrap() panics on None, returns value on Some [OK]
Common Mistakes:
  • Thinking unwrap returns None instead of panicking
  • Confusing unwrap with expect
  • Assuming unwrap converts Option to Result
2. Which of the following is the correct syntax to use expect on an Option with a custom error message?
easy
A. option.unwrap("Custom error message")
B. option.expect("Custom error message")
C. option.expect_error("Custom error message")
D. option.unwrap_or("Custom error message")

Solution

  1. Step 1: Recall expect syntax

    The expect method is called on an Option or Result with a string argument for the error message.
  2. Step 2: Identify correct method and argument

    The correct syntax is option.expect("Custom error message"). Other options are invalid method names or wrong usage.
  3. Final Answer:

    option.expect("Custom error message") -> Option B
  4. Quick Check:

    expect() takes a string message = A [OK]
Hint: expect() needs a string message inside parentheses [OK]
Common Mistakes:
  • Using unwrap with a message argument
  • Using non-existent methods like expect_error
  • Confusing unwrap_or with expect
3. What will be the output of this Rust code?
let value: Option<i32> = Some(10);
println!("{}", value.unwrap());
medium
A. None
B. Compilation error
C. panic at unwrap called on None
D. 10

Solution

  1. Step 1: Check Option value

    The variable value is Some(10), so it contains the integer 10.
  2. Step 2: unwrap() extracts the value

    Calling unwrap() on Some(10) returns 10 without panicking.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    unwrap() on Some(10) = 10 [OK]
Hint: unwrap() returns inner value if Some, else panics [OK]
Common Mistakes:
  • Expecting None as output
  • Thinking unwrap returns Option again
  • Assuming code panics on Some
4. Identify the error in this Rust code snippet:
let value: Option<i32> = None;
println!("{}", value.unwrap());
medium
A. Runtime panic because unwrap called on None
B. Compilation error due to unwrap on None
C. unwrap returns 0 by default
D. No error, prints None

Solution

  1. Step 1: Analyze Option value

    The variable value is None, meaning it has no value inside.
  2. Step 2: unwrap() behavior on None

    Calling unwrap() on None causes a runtime panic and stops the program.
  3. Final Answer:

    Runtime panic because unwrap called on None -> Option A
  4. Quick Check:

    unwrap() panics on None = D [OK]
Hint: unwrap() panics at runtime if Option is None [OK]
Common Mistakes:
  • Thinking unwrap returns None safely
  • Expecting compilation error instead of panic
  • Assuming unwrap returns default value
5. You want to safely get a value from an Option<String> but provide a clear error message if it's None. Which code snippet correctly uses expect for this?
hard
A. let name = option_name.expect("Name was missing!");
B. let name = option_name.unwrap_or("Name was missing!");
C. let name = option_name.unwrap("Name was missing!");
D. let name = option_name.expect_error("Name was missing!");

Solution

  1. Step 1: Understand expect usage

    The expect method extracts the value or panics with the provided custom message if None.
  2. Step 2: Check each option

    let name = option_name.expect("Name was missing!"); correctly uses expect with a string message. let name = option_name.unwrap_or("Name was missing!"); uses unwrap_or which returns a default value, not an error. The remaining options use invalid syntax.
  3. Final Answer:

    let name = option_name.expect("Name was missing!"); -> Option A
  4. Quick Check:

    expect() with message = A [OK]
Hint: Use expect("message") to add custom panic messages [OK]
Common Mistakes:
  • Using unwrap_or instead of expect for error message
  • Passing message to unwrap (not allowed)
  • Using non-existent expect_error method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard