Skip to main content
A trait for handling Option related operations.

Signature

Trait functions

expect

Returns the contained Some value, consuming the self value.

Signature

Panics

Panics if the option value is None with a custom felt252 panic message err.

Examples

unwrap

Returns the contained Some value, consuming the self value.

Signature

Panics

Panics if the self value equals None.

Examples

ok_or

Transforms the Option into a Result, mapping Some(v) to Ok(v) and None to Err(err).

Signature

Examples

ok_or_else

Transforms the Option into a Result, mapping Some(v) to Ok(v) and None to Err(err()).

Signature

Examples

and

Returns None if the option is None, otherwise returns optb. Arguments passed to and are eagerly evaluated; if you are passing the result of a function call, it is recommended to use and_then, which is lazily evaluated.

Signature

Examples

and_then

Returns None if the option is None, otherwise calls f with the wrapped value and returns the result. Some languages call this operation flatmap.

Signature

Examples

or

Returns the option if it contains a value, otherwise returns optb. Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use or_else, which is lazily evaluated.

Signature

Examples

or_else

Returns the option if it contains a value, otherwise calls f and returns the result.

Signature

Examples

xor

Returns Some if exactly one of self, optb is Some, otherwise returns None.

Signature

Examples

is_some

Returns true if the Option is Some, false otherwise.

Signature

Examples

is_some_and

Returns true if the Option is Some and the value inside of it matches a predicate.

Signature

Examples

is_none

Returns true if the Option is None, false otherwise.

Signature

Examples

is_none_or

Returns true if the Option is None or the value inside of it matches a predicate.

Signature

Examples

unwrap_or

Returns the contained Some value if self is Some(x). Otherwise, returns the provided default.

Signature

Examples

unwrap_or_default

Returns the contained Some value if self is Some(x). Otherwise, returns Default::::default().

Signature

Examples

unwrap_or_else

Returns the contained Some value or computes it from a closure.

Signature

Examples

map

Maps an Option to Option by applying a function to a contained value (if Some) or returns None (if None).

Signature

Examples

map_or

Returns the provided default result (if none), or applies a function to the contained value (if any). Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use map_or_else, which is lazily evaluated.

Signature

Examples

map_or_else

Computes a default function result (if none), or applies a different function to the contained value (if any).

Signature

Examples

take

Takes the value out of the option, leaving a None in its place.

Signature

Examples

filter

Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:
  • Some(t) if predicate returns true (where t is the wrapped value), and
  • None if predicate returns false.

Signature

Examples

flatten

Converts from Option> to Option.

Signature

Examples

Basic usage:
Flattening only removes one level of nesting at a time: