Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Option and Result

Option map, and_then and friends

Reference for Option transformation methods in Rust: map, map_or, map_or_else, and_then, is_some_and and inspect, with chains, closures and flattening nested options.

map and and_then transform the value inside an Option without unwrapping it by hand. If the option is None, the closure never runs and None comes out. If it is Some, the closure receives the inner value and produces the result.

The difference is the closure's return type. map takes a closure returning a plain U and wraps it again, giving Option<U>. and_then takes a closure that already returns an Option<U> and does not wrap it again, which is what you want to chain steps that can each fail.

Rust
Some(3).map(|n| n + 1);                 // Some(4)
Some("42").and_then(|s| s.parse().ok()); // Some(42)

Try it

Rust map and map_or

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Chaining fallible steps with and_then

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
impl<T> Option<T> {
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U>;
    pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U;
    pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U;
    pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U>;
    pub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool;
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}
Method Some(x) None
map(f) Some(f(x)) None
map_or(d, f) f(x) d
map_or_else(d, f) f(x) d()
and_then(f) f(x) (already an Option) None
is_some_and(f) f(x) false
inspect(f) calls f(&x), returns the option None

Returns: an Option<U> for map and and_then, a plain U for map_or and map_or_else, a bool for is_some_and.

Usage notes

  • Pick and_then when the closure returns Option. If you find Option<Option<U>> in your types, you used map where and_then belongs (or call .flatten()).
  • Closures consume. All of these take self. Calling .map on an Option<String> moves the string into the closure; use as_ref(), as_deref() or as_mut() first to work with references.
  • Method references. map(str::len), map(String::from) and and_then(|s| s.parse().ok()) keep chains tidy.
  • map_or evaluates its default eagerly, so use map_or_else when the default is costly.
  • Replacing match. A match whose None arm just returns None is a map or and_then. When both arms contain real logic, a match is clearer than a chain.
  • is_some_and replaces map_or(false, ...), and is stable since Rust 1.70. inspect was added in 1.76.
  • Errors. To keep the reason for a failure, convert with ok_or and use Result combinators instead.