Option
A value that may or may not be present. Equivalent to Go’s (T, bool).
enum Option<T> { Some(T), None,}Option.is_some()
Section titled “Option.is_some()”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 foundOption.is_none()
Section titled “Option.is_none()”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 foundOption.unwrap_or()
Section titled “Option.unwrap_or()”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"Option.unwrap_or_else()
Section titled “Option.unwrap_or_else()”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 absentOption.map()
Section titled “Option.map()”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"Option.map_or()
Section titled “Option.map_or()”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 absentOption.map_or_else()
Section titled “Option.map_or_else()”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(),)Option.and_then()
Section titled “Option.and_then()”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 nextOption.or_else()
Section titled “Option.or_else()”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 absentOption.filter()
Section titled “Option.filter()”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 nameOption.take()
Section titled “Option.take()”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 NoneOption.ok_or()
Section titled “Option.ok_or()”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 foundOption.ok_or_else()
Section titled “Option.ok_or_else()”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 absentOption.zip()
Section titled “Option.zip()”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 foundOption.flatten()
Section titled “Option.flatten()”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