Variants
Methods
Returns the contained [Some
] value, consuming the self
value.
Panics
Panics if the value is a [None
] with a custom panic message provided by msg
.
Examples
let x = Some;
assert_eq!;
let x = None;
x.expect; // panics with `fruits are healthy`
Recommended Message Style
We recommend that expect
messages are used to describe the reason you expect the Option
should be Some
.
let item = slice.get .expect;
Hint: If you're having trouble remembering how to phrase expect error messages remember to focus on the word "should" as in "env variable should be set by blah" or "the given binary should be available and executable by the current user".
For more detail on expect message styles and the reasoning behind our recommendation please refer to the section on "Common Message Styles" in the std::error
module docs.
Returns the contained [Some
] value, consuming the self
value.
Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the [None
] case explicitly, or call unwrap_or
, unwrap_or_else
, or unwrap_or_default
.
Panics
Panics if the self value equals [None
].
Examples
let x = Some;
assert_eq!;
let x = None;
assert_eq!; // fails
Returns the contained [Some
] value or a provided default
.
Arguments passed to unwrap_or
are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrap_or_else
, which is lazily evaluated.
Examples
assert_eq!;
assert_eq!;
Returns the contained [Some
] value or computes it from a closure.
Examples
let k = 10;
assert_eq!;
assert_eq!;
Returns true
if the option is a [Some
] value.
Examples
let x = Some;
assert_eq!;
let x = None;
assert_eq!;
Returns true
if the option is a [None
] value.
Examples
let x = Some;
assert_eq!;
let x = None;
assert_eq!;
Construct an iterator over an optional value.
Examples
let value = Some;
let it = value.iter;
assert_eq!;
assert_eq!;
let value = None;
let it = value.iter;
assert_eq!;
Returns [None
] if the option is [None
], otherwise calls f
with the wrapped value and returns the result.
Some languages call this operation flatmap.
Examples
assert_eq!;
assert_eq!; // overflowed!
assert_eq!;
Often used to chain fallible operations that may return [None
].
let arr_2d = ;
let item_0_1 = arr_2d.get .and_then;
assert_eq!;
let item_2_0 = arr_2d.get .and_then;
assert_eq!;
Maps an Option<T>
to Option<U>
by applying a function to a contained value (if Some
) or returns None
(if None
).
Examples
Calculates the length of an Option<[String]>
as an Option<[usize]>
, consuming the original:
let maybe_some_string = Some;
// `Option::map` takes self *by value*, consuming `maybe_some_string`
let maybe_some_len = maybe_some_string.map;
assert_eq!;
let x = None;
assert_eq!;
Takes the value out of the option, leaving a [None
] in its place.
Examples
let x = Some;
let y = x.take;
assert_eq!;
assert_eq!;
let x = None;
let y = x.take;
assert_eq!;
assert_eq!;
Transposes an Option
of a [Result
] into a [Result
] of an Option
.
[None
] will be mapped to [Ok]\([None])
. [Some]\([Ok]\(\_))
and [Some]\([Err]\(\_))
will be mapped to [Ok]\([Some]\(\_))
and [Err]\(\_)
.
Examples
let x = Ok;
let y = Some;
assert_eq!;
Transforms the Option<T>
into a [Result<T, E>
], mapping Some(v)
to Ok(v)
and [None
] to Err(err)
.
Arguments passed to ok_or
are eagerly evaluated; if you are passing the result of a function call, it is recommended to use ok_or_else
, which is lazily evaluated.
Examples
let x = Some;
assert_eq!;
let x = None;
assert_eq!;
Transforms the Option<T>
into a [Result<T, E>
], mapping Some(v)
to Ok(v)
and [None
] to Err(err())
.
Examples
let x = Some;
assert_eq!;
let x = None;
assert_eq!;
Protocols
for item in value
Construct an iterator over an optional value.
Examples
let value = Some;
let out = ;
for v in value
assert_eq!;