OptionTrait
A trait for handling Option<T>
related operations.
Fully qualified path: core::option::OptionTrait
pub trait OptionTrait<T>
Trait functions
expect
Returns the contained Some
value, consuming the self
value. # Panics Panics if the option value is None
with a custom felt252
panic message err
. # Examples
let option = Option::Some(123);
let value = option.expect('no value');
assert!(value == 123);
Fully qualified path: core::option::OptionTrait::expect
fn expect(self: Option<T>, err: felt252) -> T
unwrap
Returns the contained Some
value, consuming the self
value. # Panics Panics if the self
value equals None
. # Examples
let option = Option::Some(123);
let value = option.unwrap();
assert!(value == 123);
Fully qualified path: core::option::OptionTrait::unwrap
fn unwrap(self: Option<T>) -> T
ok_or
Transforms the Option<T>
into a Result<T, E>
, mapping Option::Some(v)
to Result::Ok(v)
and Option::None
to Result::Err(err)
. # Examples
let option = Option::Some(123);
let result = option.ok_or('no value');
assert!(result.unwrap() == 123);
Fully qualified path: core::option::OptionTrait::ok_or
fn ok_or<E, +Destruct<E>>(self: Option<T>, err: E) -> Result<T, E>
is_some
Returns true
if the Option
is Option::Some
, false
otherwise. # Examples
let option = Option::Some(123);
assert!(option.is_some());
Fully qualified path: core::option::OptionTrait::is_some
fn is_some(self: @Option<T>) -> bool
is_none
Returns true
if the Option
is Option::None
, false
otherwise. # Examples
let option = Option::Some(123);
assert!(!option.is_none());
Fully qualified path: core::option::OptionTrait::is_none
fn is_none(self: @Option<T>) -> bool
unwrap_or
Returns the contained Some
value if self
is Option::Some(x)
. Otherwise, returns the provided default. # Examples
let option = Option::Some(123);
assert!(option.unwrap_or(456) == 123);
let option = Option::None;
assert!(option.unwrap_or(456) == 456);
Fully qualified path: core::option::OptionTrait::unwrap_or
fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T
unwrap_or_default
Returns the contained Some
value if self
is Option::Some(x)
. Otherwise, returns Default::<T>::default()
. # Examples
let option = Option::Some(123);
assert!(option.unwrap_or_default() == 123);
let option: Option<felt252> = Option::None;
assert!(option.unwrap_or_default() == Default::default());
Fully qualified path: core::option::OptionTrait::unwrap_or_default
fn unwrap_or_default<+Default<T>>(self: Option<T>) -> T