Skip to content

Option

A value that may or may not be present. Equivalent to Go’s (T, bool).

enum Option<T> {
Some(T),
None,
}

Returns true if Option wraps a value.

impl<T> Option<T> {
fn is_some(self) -> bool
}
let found = users.get(id).is_some()
true if id was found

Returns true if Option is None.

impl<T> Option<T> {
fn is_none(self) -> bool
}
let missing = users.get(id).is_none()
false if id was found

Returns the wrapped value, or default if None.

impl<T> Option<T> {
fn unwrap_or(self, default: T) -> T
}
let name = users.get(id).unwrap_or("stranger")
"alice" if id was found, else "stranger"

Returns the wrapped value, or computes it from f if None.

impl<T> Option<T> {
fn unwrap_or_else(self, f: fn() -> T) -> T
}
let name = users.get(id).unwrap_or_else(|| default_name())
runs f only if id was absent

Applies f to the wrapped value, or returns None if None.

impl<T> Option<T> {
fn map<U>(self, f: fn(T) -> U) -> Option<U>
}
let length = users.get(id).map(|name| name.length())
Some(5) for "alice"

Applies f to the wrapped value, or returns default if None.

impl<T> Option<T> {
fn map_or<U>(self, default: U, f: fn(T) -> U) -> U
}
let length = users.get(id).map_or(0, |name| name.length())
5 for "alice", 0 when absent

Applies f to the wrapped value, or computes a default from default if None.

impl<T> Option<T> {
fn map_or_else<U>(self, default: fn() -> U, f: fn(T) -> U) -> U
}
let length = users.get(id).map_or_else(
runs default only if id was absent
|| 0,
|name| name.length(),
)

Calls f with the wrapped value and returns its own Option, or returns None if None.

impl<T> Option<T> {
fn and_then<U>(self, f: fn(T) -> Option<U>) -> Option<U>
}
let age = users.get(id).and_then(|name| ages.get(name))
Some(30), chaining one lookup into the next

Returns Option if it is Some, otherwise calls f.

impl<T> Option<T> {
fn or_else(self, f: fn() -> Option<T>) -> Option<T>
}
let name = users.get(id).or_else(|| Some("stranger"))
runs f only if id was absent

Returns Option if f returns true, or None otherwise.

impl<T> Option<T> {
fn filter(self, f: fn(T) -> bool) -> Option<T>
}
let named = users.get(id).filter(|name| !name.is_empty())
drops an empty name

Takes the value out of Option, leaving None in its place.

impl<T> Option<T> {
fn take(self: mut Ref<Option<T>>) -> Option<T>
}
let mut slot = users.get(id)
let taken = slot.take()
Some("alice")
let emptied = slot.is_none()
true, the slot is now None

Transforms Option<T> into Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).

impl<T> Option<T> {
fn ok_or<E>(self, err: E) -> Result<T, E>
}
let result = users.get(id).ok_or("no such user")
Ok("alice") if id was found

Transforms Option<T> into Result<T, E>, mapping Some(v) to Ok(v) and None to Err(f()).

impl<T> Option<T> {
fn ok_or_else<E>(self, f: fn() -> E) -> Result<T, E>
}
let result = users.get(id).ok_or_else(|| missing_user(id))
runs f only if id was absent

Pairs two of Option into one Option of a tuple. Returns Some((a, b)) if both are Some, otherwise None.

impl<T> Option<T> {
fn zip<U>(self, other: Option<U>) -> Option<(T, U)>
}
let pair = users.get(id).zip(age)
Some(("alice", 30)) when both were found

Converts Option<Option<U>> into Option<U>.

impl<U> Option<Option<U>> {
fn flatten(self) -> Option<U>
}
let nested = users.get(id).map(|name| ages.get(name))
let age = nested.flatten()
Some(30), one Option instead of two