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<T> / Some / None

Option<T> / Some / None

Option<T> is an enum that represents whether a value is present (Some) or absent (None). In Rust, Option is used instead of null, allowing you to handle the possibility of a missing value in a type-safe way.

Syntax

// When a value is present, wrap it in Some.
let some_value: Option<i32> = Some(42);

// When no value is present, use None.
let no_value: Option<i32> = None;

// Use match to branch on whether a value exists.
match option_value {
    Some(x) => println!("Value is {}.", x),
    None    => println!("No value."),
}

// Use if let to handle only the Some case.
if let Some(x) = option_value {
    println!("Value is {}.", x);
}

Syntax Reference

Syntax / VariantDescription
Some(value)A variant indicating a value is present. Holds exactly one value of any type.
NoneA variant indicating no value is present. An empty variant with no type parameter.
matchBranches into two patterns: Some(x) and None. Exhaustiveness checking means a compile error occurs if either pattern is missing.
if let Some(x)Syntactic sugar that runs a block only when the value is Some. Does nothing when the value is None.
while let Some(x)Continues looping as long as the value is Some. Useful for processing stacks and iterators.

Sample Code

fn find_first_even(numbers: &[i32]) -> Option<i32> {
    for &n in numbers {
        if n % 2 == 0 {
            return Some(n); // Found an even number, return it wrapped in Some.
        }
    }
    None // No even number found, return None.
}

fn main() {
    let nums = vec![1, 3, 5, 4, 7];

    // Branch using match.
    match find_first_even(&nums) {
        Some(n) => println!("First even number: {}", n),
        None    => println!("No even numbers found."),
    }

    // Use if let to handle only the Some case.
    if let Some(n) = find_first_even(&nums) {
        println!("if let: {}", n);
    }

    // Check against an empty list.
    let empty: Vec<i32> = vec![];
    match find_first_even(&empty) {
        Some(n) => println!("Even number: {}", n),
        None    => println!("The list is empty."),
    }

    // Use while let to process the stack until it is empty.
    let mut stack = vec![Some(1), Some(2), None, Some(3)];
    while let Some(item) = stack.pop() {
        if let Some(n) = item {
            println!("Stack: {}", n);
        }
    }
}

Overview

Option<T> is an enum defined in the Rust standard library that represents the concept of null or nil found in other languages, but in a type-safe way. Attempting to extract a value when it is None causes a panic, so always check for a value first using match or if let.

Option is widely used as a function return type when a value might not be found. For example, many standard library APIs such as HashMap::get(), Vec::first(), and string search methods return Option. By using Option, the Rust compiler enforces value existence checks, preventing runtime errors like NullPointerException.

If you find any errors or copyright issues, please .