Optional value
Optional values can either contain a value or they can be empty.
See also module-level documentation.
Example
let a = Option::some(1);
let b: Option<i32> = Option::none();
assert_eq!(a.is_some(), true);
assert_eq!(b.is_some(), false);
Run this example
Methods
impl Option<T> { ... }
-
fn some(inner: T) -> Option<T>
Create a populated
Option
valueExample
let opt = Option::some(42); assert!(opt.is_some()); assert_eq!(opt.unwrap(), 42);
Run this example -
fn none() -> Option<T>
Create an empty
Option
valueExample
let opt: Option<i32> = Option::none(); assert!(opt.is_none());
Run this example -
Returns
true
if the option is populated,false
otherwise. -
Returns
true
if the option is empty,false
otherwise. -
fn unwrap(self: Option<T>) -> T
Returns a value, if present, panics otherwise.
Examples
assert_eq!(Option::some(42).unwrap(), 42);
Run this examplelet opt: Option<i32> = Option::none(); opt.unwrap(); // panics
Run this example -
Convert the option into a Result.
If the option is empty, the
err
value will be used for the error variant. -
Convert the option into a Result.
If the option is empty, the callback will be called to get the the error variant.
-
fn unwrap_or(self: Option<T>, val: T) -> T
Returns a value, if present, otherwise return a default value.
Example
assert_eq!(Option::some(42).unwrap_or(1337), 42); assert_eq!(Option::none::<i32>().unwrap_or(1337), 1337);
Run this example -
fn unwrap_or_else<F>(self: Option<T>, func: F) -> T
F: Fn() -> TReturns a value, if present, otherwise invoke func to get the default.
Example
let a = Option::some(42); let b: Option<i32> = Option::none(); fn default() -> i32 { 1337 } assert_eq!(a.unwrap_or_else(default), 42); assert_eq!(b.unwrap_or_else(default), 1337);
Run this example -
Transform the value inside the
Option
by calling the provided function.If the value is none, function is not called.
Example
let a = Option::some(42).map(|v: i32| -> i32 { v + 1 }); let b = Option::none::<i32>().map(|v: i32| -> i32 { v + 1 }); assert_eq!(a, Option::some(43)); assert_eq!(b, Option::none());
Run this example -
If
self
is none, return none, otherwise returnother
Example
let a = Option::some(42); let b = Option::some(true); let c = Option::none::<i32>(); assert_eq!(a.and(b), Option::some(true)); assert_eq!(a.and(c), Option::none()); assert_eq!(c.and(a), Option::none());
Run this example -
If
self
is none, return none, otherwise return the result of callingfunc
.Example
let a = Option::some(42) .and_then(|v: i32| -> Option<i32> { if v == 0 { Option::none() } else { Option::some(v + 1) } }); assert_eq!(a, Option::some(43));
Run this example -
Return
self
if it is populated, otherwise returnother
.Example
let a = Option::some(42); let b = Option::none::<i32>(); assert_eq!(a.or(b), Option::some(42)); assert_eq!(b.or(a), Option::some(42));
Run this example -
Return
self
if it is populated, otherwise return the result of callingfunc
.Example
let a = Option::some(42); let b = Option::none::<i32>(); assert_eq!( a.or_else(|| -> Option<i32> { Option::none() }), Option::some(42) ); assert_eq!( b.or_else(|| -> Option<i32> { Option::some(42) }), Option::some(42) );
Run this example -
Replaces the value in the option by the value given in parameter, returning the old value if present.
Example
let a = Option::some(42); let b = Option::none::<i32>(); assert_eq!(a.replace(1337), Option::some(42)); assert_eq!(b.replace(1337), Option::none()); assert_eq!(a, Option::some(1337)); assert_eq!(b, Option::some(1337));
Run this example -
fn as_nullable_ptr(self: &Option<T>) -> &T
Returns pointer to inner or a null pointer if not present.
Example
let a = Option::some(42); let b = Option::none::<i32>(); assert_eq!(*a.as_nullable_ptr(), 42); assert_eq!(b.as_nullable_ptr(), null);
Run this example -
Transform a pointer to
Option
to anOption
of pointer.Example
let a = Option::some(42); let b = a.as_ptr(); assert_eq!(*b.unwrap(), 42); a = Option::some(1337); assert_eq!(*b.unwrap(), 1337);
Run this example -
Transform a mutable pointer to
Option
to anOption
of mutable pointer.Example
let a = Option::some(42); *a.as_mut_ptr().unwrap() = 1337; assert_eq!(a, Option::some(1337));
Run this example -
Flattens a nested
Option
into a singleOption
.Example
assert_eq!(Option::some(Option::some(42)).flatten(), Option::some(42)); assert_eq!(Option::some(Option::none::<i32>()).flatten(), Option::none()); assert_eq!(Option::none::<Option<i32>>().flatten(), Option::none());
Run this example -
Merges two
Option
s into anOption
of a tuple.If either
self
orother
is none, the result is none.Example
let a = Option::some(42); let b = Option::some(1337); assert_eq!(a.zip(b), Option::some((42, 1337)));
Run this example -
fn iter(self: &Option<T>) -> OnceIterator<T>
impl Option { ... }
-
Convert
Option<Result<T, E>>
toResult<Option<T>, E>
See also transpose for the reverse operation.
Example
assert_eq!( Option::some(Result::ok::<i32, ()>(42)).transpose(), Result::ok(Option::some(42)) ); assert_eq!( Option::some(Result::err::<i32, ()>(())).transpose(), Result::err(()) ); assert_eq!( Option::none::<Result<i32, ()>>().transpose(), Result::ok(Option::none()) );
Run this example -
fn compare<T>(lhs: &Option<T>, rhs: &Option<T>) -> Ordering
T: Comparable<T> -
fn fmt<T, F>(self: &Option<T>, formatter: &mut F) -> Result
T: Formattable<T, F>F: Formatter<F> -
Frees the memory backing the object.
Mixins
impl Option { ... }
-
-
Returns
false
if arguments are equal,true
otherwise -
mixin<T> Comparable<Option<T>>
T: Comparable<T> -
Returns
true
iflhs
is strictly less thanrhs
,false
otherwise -
Returns
true
iflhs
is less or equal torhs
,false
otherwise -
Returns
true
iflhs
strictly greater thanrhs
,false
otherwise -
Returns
true
iflhs
greater than or equalrhs
,false
otherwise