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::map() / and_then() / is_some()

Option::map() / and_then() / is_some()

A set of methods for transforming and chaining Option values. Instead of extracting a value before processing it, you can chain transformations directly on the Option, writing concise logic while handling presence or absence at each step.

Syntax

// Transforms the value inside Some using a closure; None is passed through unchanged.
let mapped = option.map(|x| x * 2);

// Applies a closure that returns an Option when the value is Some (flattens the result).
let chained = option.and_then(|x| some_func(x));

// Returns the alternative Option when the value is None.
let fallback = option.or(Some(42));

// Calls a closure to produce an alternative Option when the value is None.
let fallback = option.or_else(|| Some(compute()));

// Returns true if the value is Some.
let has_value = option.is_some();

// Returns true if the value is None.
let is_empty = option.is_none();

Method List

MethodDescription
map(fn)When the value is Some(x), calls fn(x) and wraps the result in Some. None is returned as-is. Use this to transform the type of the contained value.
and_then(fn)When the value is Some(x), calls fn(x) and returns its Option result directly. Use this to chain operations that themselves return an Option.
or(opt)Returns the original value if it is Some, or returns the argument opt if it is None. Use this to provide a fallback value.
or_else(fn)Calls the closure and returns its result only when the value is None.
is_some()Returns true if the value is Some. Useful for concise conditional checks.
is_none()Returns true if the value is None.
filter(fn)When the value is Some(x), converts it to None if the predicate closure returns false.

Sample Code

fn parse_positive(s: &str) -> Option<u32> {
    s.parse::<i32>().ok()                // Parses the string into an integer.
        .and_then(|n| if n > 0 { Some(n as u32) } else { None }) // Keeps only positive numbers.
}

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

    // Use map to transform the value.
    println!("{:?}", some_val.map(|x| x * 10)); // Some(50)
    println!("{:?}", none_val.map(|x| x * 10)); // None

    // Use and_then to chain a function that returns an Option.
    println!("{:?}", parse_positive("10")); // Some(10)
    println!("{:?}", parse_positive("-5")); // None
    println!("{:?}", parse_positive("abc")); // None

    // Use or to fall back when the value is None.
    println!("{:?}", none_val.or(Some(99)));   // Some(99)
    println!("{:?}", some_val.or(Some(99)));   // Some(5)

    // Use filter to convert a Some that fails the condition into None.
    println!("{:?}", some_val.filter(|&x| x > 3)); // Some(5)
    println!("{:?}", some_val.filter(|&x| x > 10)); // None

    // Use is_some / is_none to check the presence of a value.
    println!("{}", some_val.is_some()); // true
    println!("{}", none_val.is_none()); // true
}

Notes

The difference between map() and and_then() lies in what the closure returns. Use map() when the closure returns a plain value, and and_then() when it returns an Option. If the closure passed to and_then() does not return an Option, a compile error will occur.

Chaining these methods lets you express "transform if present → check condition → transform again" as a pipeline without nesting multiple match expressions. In Rust, this style is called the combinator pattern, drawing on ideas from functional programming.

If you find any errors or copyright issues, please .