Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The generic enum Option is defined inside the standard library and is used everywhere in Rust to handle the Value/No-value case. It allows to declare and handle the possiblity of the absence of value.

Some of the methods of Option<T> are presented in Table 1. See Option for a full list.

Table 1:Some methods of Option<T>

Method

Description

and(other)

Returns None if this Option is None otherwise returns other.

and_then(f)

Returns None if this Option is None otherwise calls f() with the wrapped value.

cloned()

Convert an Option<&T> into an Option<T> by cloning.

copied()

Convert an Option<&T> into an Option<T> by copying.

expect(m)

Returns T if Some(T), or panic with the custom message m if None.

filter(p)

Returns None if None, and Some(t) if predicates p evaluates to true on t.

flatten()

Returns Option<T> if the object is a Option<Option<T>>.

get_or_insert(v)

Sets value to v if None, then returns a mutable reference to the internal value.

insert(v)

Sets value to v and returns a mutable reference to the internal value.

inspect(f)

Calls function f() on the contained value, then returns this Option<T> instance.

is_none()

Returns true if None.

is_some()

Returns true if Some(v).

iter()

Returns an iterator on the possibly contained value.

iter_mut()

Returns a mutable iterator on the possibly contained value.

map(f)

Maps the function f() on the contained value if any or returns None.

map_or(v, f)

Maps the function f() on the contained value if any or returns v.

ok_or(e)

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

ok_or_else(f)

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

or(other)

Returns this option if it contains a value, otherwise returns other.

or_else(f)

Returns this option if it contains a value, otherwise calls f() and returns its value.

replace(v)

Sets object to Some(v) and returns the old value, if any.

take()

Returns the current value as Some(v) or None, and leaves only None.

take_if(p)

Same as take() but only if predicate p() is true.

transpose()

Transposes an Option<Result<T, E>> into a Result<Option<T>, E>.

unwrap()

Returns the current value v if Some(v), or panics if None.

unwrap_or(u)

Returns the current value v if Some(v), or u if None.

unwrap_or_else(f)

Returns the current value v if Some(v), or executes the function f and returns its result.

unzip()

Unzips an option of a tuple of two values into a tuple of two options.

xor(other)

Returns the Some(v) of either this option or other, or None if both are None or Some.

zip(other)

Makes an option of a tuple by combining this option and other.

The Option<T> type is a generic enum type (see Generic enum) that defines a None variant that represents the No-value case, and a Some(T) variant that represents a value.

Here the definition of Option<T> inside the standard library:

In the following example, the same variable n, declared as an Option<i32>, can be set to either None or Some(...):

None
Some(-1230)

17.1.1is_none() & is_some()

We may check if an Option<T> instance has a value or not with the following methods:

(false, true)

17.1.2unwrap() & expect()

We may try to get the possible value stored in an Option<T> by using the unwrap() method. If there is a value, then we get it:

10

Otherwise, we get a panic:

panicked at src/lib.rs:116:3:

The expect() method works in the same way, but allows us to define a custom panic message:

panicked at src/lib.rs:114:3:

17.1.3Returning an Option

Many functions return an Option<T> in order to handle the possibility of absence of value.

For instance the find() method of the str type that searches for a pattern inside a string returns an Option<usize> in order to handle the case of no match. Its declaration is as follow:

It returns None when no match is found:

(Some(2), None)

see solution

17.1.4Usage with if/let & co

Using if/let we can get the eventual value contained in an Option and run some code with it:

Couldn't automatically determine type of variable `x`.
Please give it an explicit type.

In the following example, we loop on all elements of x, taking them one by one until there is no element left and pop returns None:

see solution

see solution