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 |
|---|---|
| Returns |
| Returns |
| Convert an |
| Convert an |
| Returns |
| Returns |
| Returns |
| Sets value to |
| Sets value to |
| Calls function |
| Returns |
| Returns |
| Returns an iterator on the possibly contained value. |
| Returns a mutable iterator on the possibly contained value. |
| Maps the function |
| Maps the function |
| Transforms the |
| Transforms the |
| Returns this option if it contains a value, otherwise returns
|
| Returns this option if it contains a value, otherwise calls |
| Sets object to |
| Returns the current value as |
| Same as |
| Transposes an |
| Returns the current value |
| Returns the current value |
| Returns the current value |
| Unzips an option of a tuple of two values into a tuple of two options. |
| Returns the |
| Makes an option of a tuple by combining this option and |
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:
enum Option<T> {
Some(T),
None,
}In the following example, the same variable n, declared as an
Option<i32>, can be set to either None or Some(...):
let mut n: Option<i32> = None;
println!("{n:?}");
n = Some(-1230);
println!("{n:?}");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:
let x: Option::<u8> = Some(10);
(x.is_none(), x.is_some())(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:
let x: Option::<u8> = Some(10);
x.unwrap()10Otherwise, we get a panic:
let x: Option::<u8> = None;
x.unwrap()panicked at src/lib.rs:116:3:
The expect() method works in the same way, but allows us to define a
custom panic message:
let x: Option::<u8> = None;
x.expect("A value is required !")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:
fn find<P>(&self, pat: P) -> Option<usize> {
...
}It returns None when no match is found:
("abcdef".find('c'), "ghijkl".find('c'))(Some(2), None)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:
let x = Some(10u8);
if let Some(v) = x {
println!("My value is {v}");
}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:
let mut i = 3..10;
while let Some(n) = i.next() {
print!("{n} ");
}
println!();