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. # PanicsPanics if the option value is None
with a custom felt252
panic message err
. # Examples
let option = Some(123);
let value = option.expect('no value');
assert!(value == 123);
Fully qualified path: core::option::OptionTrait::expect
const fn expect(self: Option<T>, err: felt252) -> T
unwrap
Returns the contained Some
value, consuming the self
value. # PanicsPanics if the self
value equals None
. # Examples
let option = Some(123);
let value = option.unwrap();
assert!(value == 123);
Fully qualified path: core::option::OptionTrait::unwrap
const fn unwrap(self: Option<T>) -> T
ok_or
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err)
. # Examples
assert_eq!(Some('foo').ok_or(0), Ok('foo'));
let option: Option<felt252> = None;
assert_eq!(option.ok_or(0), Err(0));
Fully qualified path: core::option::OptionTrait::ok_or
fn ok_or<E, +Destruct<E>>(self: Option<T>, err: E) -> Result<T, E>
ok_or_else
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err())
. # Examples
assert_eq!(Some('foo').ok_or_else(|| 0), Ok('foo'));
let option: Option<felt252> = None;
assert_eq!(option.ok_or_else(|| 0), Err(0));
Fully qualified path: core::option::OptionTrait::ok_or_else
fn ok_or_else<E, F, +Destruct<E>, +core::ops::FnOnce<F, ()>[Output: E], +Drop<F>>(
self: Option<T>, err: F,
) -> Result<T, E>
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. # Examples
let x = Some(2);
let y: Option<ByteArray> = None;
assert_eq!(x.and(y), None);
let x: Option<u32> = None;
let y: Option<ByteArray> = Some("foo");
assert_eq!(x.and(y), None);
let x = Some(2);
let y: Option<ByteArray> = Some("foo");
assert_eq!(x.and(y), Some("foo"));
let x: Option<u32> = None;
let y: Option<ByteArray> = None;
assert_eq!(x.and(y), None);
Fully qualified path: core::option::OptionTrait::and
fn and<U, +Drop<T>, +Drop<U>>(self: Option<T>, optb: Option<U>) -> Option<U>
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. # Examples
use core::num::traits::CheckedMul;
let option: Option<ByteArray> = checked_mul(2_u32, 2_u32)
.and_then(|v| Some(format!("{}", v)));
assert_eq!(option, Some("4"));
let option: Option<ByteArray> = checked_mul(65536_u32, 65536_u32)
.and_then(|v| Some(format!("{}", v)));
assert_eq!(option, None); // overflowed!
let option: Option<ByteArray> = Option::<u32>::None
.and_then(|v| Some(format!("{}", v)));
assert_eq!(option, None);
Fully qualified path: core::option::OptionTrait::and_then
fn and_then<U, F, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: Option<U>]>(
self: Option<T>, f: F,
) -> Option<U>
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. # Examples
let x = Some(2);
let y = None;
assert_eq!(x.or(y), Some(2));
let x = None;
let y = Some(100);
assert_eq!(x.or(y), Some(100));
let x = Some(2);
let y = Some(100);
assert_eq!(x.or(y), Some(2));
let x: Option<u32> = None;
let y = None;
assert_eq!(x.or(y), None);
Fully qualified path: core::option::OptionTrait::or
fn or<+Drop<T>>(self: Option<T>, optb: Option<T>) -> Option<T>
or_else
Returns the option if it contains a value, otherwise calls f
and returns the result. # Examples
let nobody = || Option::<ByteArray>::None;
let vikings = || Option::<ByteArray>::Some("vikings");
assert_eq!(Some("barbarians").or_else(vikings), Some("barbarians"));
assert_eq!(None.or_else(vikings), Some("vikings"));
assert_eq!(None.or_else(nobody), None);
Fully qualified path: core::option::OptionTrait::or_else
fn or_else<F, +Drop<F>, +core::ops::FnOnce<F, ()>[Output: Option<T>]>(
self: Option<T>, f: F,
) -> Option<T>
xor
Returns Some
if exactly one of self
, optb
is Some
, otherwise returns None
. # Examples
let x = Some(2);
let y: Option<u32> = None;
assert_eq!(x.xor(y), Some(2));
let x: Option<u32> = None;
let y = Some(2);
assert_eq!(x.xor(y), Some(2));
let x = Some(2);
let y = Some(2);
assert_eq!(x.xor(y), None);
let x: Option<u32> = None;
let y: Option<u32> = None;
assert_eq!(x.xor(y), None);
Fully qualified path: core::option::OptionTrait::xor
fn xor<+Drop<T>>(self: Option<T>, optb: Option<T>) -> Option<T>
is_some
Returns true
if the Option
is Some
, false
otherwise. # Examples
let option = Some(123);
assert!(option.is_some());
Fully qualified path: core::option::OptionTrait::is_some
fn is_some(self: @Option<T>) -> bool
is_some_and
Returns true
if the Option
is Some
and the value inside of it matches a predicate. # Examples
assert_eq!(Some(2_u8).is_some_and(|x| x > 1), true);
assert_eq!(Some(0_u8).is_some_and(|x| x > 1), false);
let option: Option<u8> = None;
assert_eq!(option.is_some_and(|x| x > 1), false);
Fully qualified path: core::option::OptionTrait::is_some_and
fn is_some_and<F, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: bool]>(
self: Option<T>, f: F,
) -> bool
is_none
Returns true
if the Option
is None
, false
otherwise. # Examples
let option = Some(123);
assert!(!option.is_none());
Fully qualified path: core::option::OptionTrait::is_none
fn is_none(self: @Option<T>) -> bool
is_none_or
Returns true
if the Option
is None
or the value inside of it matches a predicate. # Examples
assert_eq!(Some(2_u8).is_none_or(|x| x > 1), true);
assert_eq!(Some(0_u8).is_none_or(|x| x > 1), false);
let option: Option<u8> = None;
assert_eq!(option.is_none_or(|x| x > 1), true);
Fully qualified path: core::option::OptionTrait::is_none_or
fn is_none_or<F, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: bool]>(self: Option<T>, f: F) -> bool
unwrap_or
Returns the contained Some
value if self
is Some(x)
. Otherwise, returns the provided default. # Examples
let option = Some(123);
assert!(option.unwrap_or(456) == 123);
let option = None;
assert!(option.unwrap_or(456) == 456);
Fully qualified path: core::option::OptionTrait::unwrap_or
const fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T
unwrap_or_default
Returns the contained Some
value if self
is Some(x)
. Otherwise, returns Default::<T>::default()
. # Examples
let option = Some(123);
assert!(option.unwrap_or_default() == 123);
let option: Option<felt252> = 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
unwrap_or_else
Returns the contained Some
value or computes it from a closure. # Examples
let k = 10;
assert!(Some(4).unwrap_or_else(|| 2 * k) == 4);
assert!(None.unwrap_or_else(|| 2 * k) == 20);
Fully qualified path: core::option::OptionTrait::unwrap_or_else
fn unwrap_or_else<F, +Drop<F>, impl func: core::ops::FnOnce<F, ()>[Output: T], +Drop<func::Output>>(
self: Option<T>, f: F,
) -> T
map
Maps an Option<T>
to Option<U>
by applying a function to a contained value (if Some
) or returns None
(if None
). # Examples
let maybe_some_string: Option<ByteArray> = Some("Hello, World!");
// `Option::map` takes self *by value*, consuming `maybe_some_string`
let maybe_some_len = maybe_some_string.map(|s: ByteArray| s.len());
assert!(maybe_some_len == Some(13));
let x: Option<ByteArray> = None;
assert!(x.map(|s: ByteArray| s.len()) == None);
Fully qualified path: core::option::OptionTrait::map
fn map<U, F, +Destruct<F>, +core::ops::FnOnce<F, (T,)>[Output: U]>(
self: Option<T>, f: F,
) -> Option<U>
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. # Examples
assert_eq!(Some("foo").map_or(42, |v: ByteArray| v.len()), 3);
let x: Option<ByteArray> = None;
assert_eq!(x.map_or(42, |v: ByteArray| v.len()), 42);
Fully qualified path: core::option::OptionTrait::map_or
fn map_or<U, F, +Drop<U>, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: U]>(
self: Option<T>, default: U, f: F,
) -> U
map_or_else
Computes a default function result (if none), or applies a different function to the contained value (if any). # Basic examples
let k = 21;
let x = Some("foo");
assert_eq!(x.map_or_else( || 2 * k, |v: ByteArray| v.len()), 3);
let x: Option<ByteArray> = None;
assert_eq!(x.map_or_else( || 2 * k, |v: ByteArray| v.len()), 42);
Fully qualified path: core::option::OptionTrait::map_or_else
fn map_or_else<
U,
D,
F,
+Drop<U>,
+Drop<D>,
+Drop<F>,
+core::ops::FnOnce<D, ()>[Output: U],
+core::ops::FnOnce<F, (T,)>[Output: U],
>(
self: Option<T>, default: D, f: F,
) -> U
take
Takes the value out of the option, leaving a None
in its place. # Examples
let mut x = Some(2);
let y = x.take();
assert_eq!(x, None);
assert_eq!(y, Some(2));
let mut x: Option<u32> = None;
let y = x.take();
assert_eq!(x, None);
assert_eq!(y, None);
Fully qualified path: core::option::OptionTrait::take
fn take(ref self: Option<T>) -> Option<T>
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
. # Example
let is_even = |n: @u32| -> bool {
*n % 2 == 0
};
assert_eq!(None.filter(is_even), None);
assert_eq!(Some(3).filter(is_even), None);
assert_eq!(Some(4).filter(is_even), Some(4));
Fully qualified path: core::option::OptionTrait::filter
fn filter<P, +core::ops::FnOnce<P, (@T,)>[Output: bool], +Destruct<T>, +Destruct<P>>(
self: Option<T>, predicate: P,
) -> Option<T>
flatten
Converts from Option<Option<T>>
to Option<T>
. # ExamplesBasic usage:
let x: Option<Option<u32>> = Some(Some(6));
assert_eq!(Some(6), x.flatten());
let x: Option<Option<u32>> = Some(None);
assert_eq!(None, x.flatten());
let x: Option<Option<u32>> = None;
assert_eq!(None, x.flatten());
Flattening only removes one level of nesting at a time:
let x: Option<Option<Option<u32>>> = Some(Some(Some(6)));
assert_eq!(Some(Some(6)), x.flatten());
assert_eq!(Some(6), x.flatten().flatten());
Fully qualified path: core::option::OptionTrait::flatten
fn flatten(self: Option<Option<T>>) -> Option<T>