Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Rust Dictionary

  1. Home
  2. Rust Dictionary
  3. Option::unwrap() / expect() / unwrap_or()

Option::unwrap() / expect() / unwrap_or()

These are methods for extracting a value from an Option. unwrap() is the simplest, but it panics on None. Use unwrap_or() to provide a fallback value, or expect() to include an error message, depending on the situation.

Syntax

// Returns the value if Some, panics if None.
let value = option.unwrap();

// Panics with a custom error message if None.
let value = option.expect("error message");

// Returns a default value if None.
let value = option.unwrap_or(default_value);

// Calls a closure to produce a default value if None.
let value = option.unwrap_or_else(|| expression);

// Returns the Default trait value if None.
let value = option.unwrap_or_default();

Method List

MethodDescription
unwrap()Returns x if the value is Some(x). Panics if the value is None. Avoid using this unless you are certain the value exists.
expect("msg")Same as unwrap(), but displays the specified message when it panics. Useful for identifying the cause during debugging.
unwrap_or(val)Returns x if the value is Some(x), or the argument val if it is None. The argument is always evaluated.
unwrap_or_else(fn)Executes the closure only when the value is None to produce a default value. Useful when computing the default value is expensive.
unwrap_or_default()Returns the Default trait value when None (e.g., 0 for numbers, an empty string for strings).

Sample Code

fn main() {
    let some_val: Option<i32> = Some(42);
    let none_val: Option<i32> = None;

    // unwrap: extracts the value when Some.
    println!("{}", some_val.unwrap()); // 42

    // expect: panics with the specified message when None.
    println!("{}", some_val.expect("value is required")); // 42

    // unwrap_or: returns the default value when None.
    println!("{}", none_val.unwrap_or(0)); // 0
    println!("{}", some_val.unwrap_or(0)); // 42

    // unwrap_or_else: the closure runs only when None.
    let result = none_val.unwrap_or_else(|| {
        println!("closure executed");
        -1
    });
    println!("{}", result); // -1

    // unwrap_or_default: the default for i32 is 0.
    let default_val: i32 = none_val.unwrap_or_default();
    println!("{}", default_val); // 0

    // The default for String is an empty string.
    let name: Option<String> = None;
    println!("'{}'", name.unwrap_or_default()); // ''
}

Overview

Choose the right method based on whether None is possible, whether a fallback value is needed, and whether computing that fallback is expensive. Avoid using unwrap() in production code, as it panics immediately on None.

Use unwrap_or() for a fixed fallback value, unwrap_or_else() when computation is needed, and unwrap_or_default() when the type's default value is acceptable. When a panic is acceptable, prefer expect() over unwrap() so the cause is easier to identify.

If you find any errors or copyright issues, please .