ResultTrait
Fully qualified path: core::result::ResultTrait
pub trait ResultTrait<T, E>
Trait functions
expect
Returns the contained Ok
value, consuming the self
value. # PanicsPanics if the value is an Err
, with the provided felt252
panic message. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(result.expect('no value') == 123);
Fully qualified path: core::result::ResultTrait::expect
const fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T
unwrap
Returns the contained Ok
value, consuming the self
value. # PanicsPanics if the value is an Err
, with a standard Result::unwrap failed
panic message. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(result.unwrap() == 123);
Fully qualified path: core::result::ResultTrait::unwrap
const fn unwrap<+Destruct<E>>(self: Result<T, E>) -> T
unwrap_or
Returns the contained Ok
value or a provided default. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(result.unwrap_or(456) == 123);
let result: Result<felt252, felt252> = Err('no value');
assert!(result.unwrap_or(456) == 456);
Fully qualified path: core::result::ResultTrait::unwrap_or
const fn unwrap_or<+Destruct<T>, +Destruct<E>>(self: Result<T, E>, default: T) -> T
unwrap_or_default
Returns the contained Ok
value or Default::<T>::default()
. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(result.unwrap_or_default() == 123);
let result: Result<felt252, felt252> = Err('no value');
assert!(result.unwrap_or_default() == 0);
Fully qualified path: core::result::ResultTrait::unwrap_or_default
fn unwrap_or_default<+Destruct<E>, +Default<T>>(self: Result<T, E>) -> T
unwrap_or_else
Returns the contained Ok
value or computes it from a closure. # Examples
assert!(Ok(2).unwrap_or_else(|e: ByteArray| e.len()) == 2);
assert!(Err("foo").unwrap_or_else(|e: ByteArray| e.len()) == 3);
Fully qualified path: core::result::ResultTrait::unwrap_or_else
fn unwrap_or_else<F, +Destruct<E>, +Drop<F>, +core::ops::FnOnce<F, (E,)>[Output: T]>(
self: Result<T, E>, f: F,
) -> T
and
Returns other
if the result is Ok
, otherwise returns the Err
value of self
. # Examples
let x: Result<u32, ByteArray> = Ok(2);
let y: Result<ByteArray, ByteArray> = Err("late error");
assert!(x.and(y) == Err("late error"));
let x: Result<u32, ByteArray> = Err("early error");
let y: Result<ByteArray, ByteArray> = Ok("foo");
assert!(x.and(y) == Err("early error"));
let x: Result<u32, ByteArray> = Err("not a 2");
let y: Result<ByteArray, ByteArray> = Err("late error");
assert!(x.and(y) == Err("not a 2"));
let x: Result<u32, ByteArray> = Ok(2);
let y: Result<ByteArray, ByteArray> = Ok("different result type");
assert!(x.and(y) == Ok("different result type"));
Fully qualified path: core::result::ResultTrait::and
fn and<U, +Destruct<T>, +Drop<E>, +Drop<U>>(self: Result<T, E>, other: Result<U, E>) -> Result<U, E>
and_then
Calls op
if the result is Ok
, otherwise returns the Err
value of self
.This function can be used for control flow based on Result
values. # Examples
use core::num::traits::CheckedMul;
fn sq_then_string(x: u32) -> Result<ByteArray, ByteArray> {
let res = x.checked_mul(x).ok_or("overflowed");
res.and_then(|v| Ok(format!("{}", v)))
}
let x = sq_then_string(4);
assert!(x == Ok("16"));
let y = sq_then_string(65536);
assert!(y == Err("overflowed"));
Fully qualified path: core::result::ResultTrait::and_then
fn and_then<U, F, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: Result<U, E>]>(
self: Result<T, E>, op: F,
) -> Result<U, E>
or
Returns other
if the result is Err
, otherwise returns the Ok
value of self
. # Examples
let x: Result<u32, ByteArray> = Ok(2);
let y: Result<u32, ByteArray> = Err("late error");
assert!(x.or(y) == Ok(2));
let x: Result<u32, ByteArray> = Err("early error");
let y: Result<u32, ByteArray> = Ok(2);
assert!(x.or(y) == Ok(2));
let x: Result<u32, ByteArray> = Err("not a 2");
let y: Result<u32, ByteArray> = Err("late error");
assert!(x.or(y) == Err("late error"));
let x: Result<u32, ByteArray> = Ok(2);
let y: Result<u32, ByteArray> = Ok(100);
assert!(x.or(y) == Ok(2));
Fully qualified path: core::result::ResultTrait::or
fn or<F, +Drop<T>, +Drop<F>, +Destruct<E>>(self: Result<T, E>, other: Result<T, F>) -> Result<T, F>
or_else
Calls op
if the result is Err
, otherwise returns the Ok
value of self
.This function can be used for control flow based on result values. # Examples
let x: Result::<u32, ByteArray> = Result::<u32, ByteArray>::Err("bad input")
.or_else(|_e| Ok(42));
assert!(x == Ok(42));
let y: Result::<u32, ByteArray> = Result::<u32, ByteArray>::Err("bad input")
.or_else(|_e| Err("not 42"));
assert!(y == Err("not 42"));
let z: Result::<u32, ByteArray> = Result::<u32, ByteArray>::Ok(100)
.or_else(|_e| Ok(42));
assert!(z == Ok(100));
Fully qualified path: core::result::ResultTrait::or_else
fn or_else<F, O, +Drop<O>, +core::ops::FnOnce<O, (E,)>[Output: Result<T, F>]>(
self: Result<T, E>, op: O,
) -> Result<T, F>
expect_err
Returns the contained Err
value, consuming the self
value. # PanicsPanics if the value is an Ok
, with the provided felt252
panic message. # Examples
let result: Result<felt252, felt252> = Err('no value');
assert!(result.expect_err('result is ok') == 'no value');
Fully qualified path: core::result::ResultTrait::expect_err
const fn expect_err<+PanicDestruct<T>>(self: Result<T, E>, err: felt252) -> E
unwrap_err
Returns the contained Err
value, consuming the self
value. # PanicsPanics if the value is an Ok
, with a standard Result::unwrap_err failed.
panic message. # Examples
let result: Result<felt252, felt252> = Err('no value');
assert!(result.unwrap_err() == 'no value');
Fully qualified path: core::result::ResultTrait::unwrap_err
const fn unwrap_err<+PanicDestruct<T>>(self: Result<T, E>) -> E
is_ok
Returns true
if the Result
is Ok
. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(result.is_ok());
Fully qualified path: core::result::ResultTrait::is_ok
fn is_ok(self: @Result<T, E>) -> bool
is_err
Returns true
if the Result
is Err
. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(!result.is_err());
Fully qualified path: core::result::ResultTrait::is_err
fn is_err(self: @Result<T, E>) -> bool
into_is_ok
Returns true
if the Result
is Ok
, and consumes the value. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(result.into_is_ok());
Fully qualified path: core::result::ResultTrait::into_is_ok
fn into_is_ok<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> bool
into_is_err
Returns true
if the Result
is Err
, and consumes the value. # Examples
let result: Result<felt252, felt252> = Ok(123);
assert!(!result.into_is_err());
Fully qualified path: core::result::ResultTrait::into_is_err
fn into_is_err<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> bool
ok
Converts from Result<T, E>
to Option<T>
.Converts self
into an Option<T>
, consuming self
, and discarding the error, if any. # Examples
let x: Result<u32, ByteArray> = Ok(2);
assert!(x.ok() == Some(2));
let x: Result<u32, ByteArray> = Err("Nothing here");
assert!(x.ok().is_none());
Fully qualified path: core::result::ResultTrait::ok
fn ok<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> Option<T>
err
Converts from Result<T, E>
to Option<E>
.Converts self
into an Option<E>
, consuming self
, and discarding the success value, if any. # Examples
let x: Result<u32, ByteArray> = Err("Nothing here");
assert!(x.err() == Some("Nothing here"));
let x: Result<u32, ByteArray> = Ok(2);
assert!(x.err().is_none());
Fully qualified path: core::result::ResultTrait::err
fn err<+Destruct<T>, +Destruct<E>>(self: Result<T, E>) -> Option<E>
map
Maps a Result<T, E>
to Result<U, E>
by applying a function to a contained Ok
value, leaving an Err
value untouched.This function can be used to compose the results of two functions. # ExamplesPrint the square of the number contained in the Result
, otherwise print the error.
let inputs: Array<Result<u32, ByteArray>> = array![
Ok(1), Err("error"), Ok(3), Ok(4),
];
for i in inputs {
match i.map(|i| i * 2) {
Ok(x) => println!("{x}"),
Err(e) => println!("{e}"),
}
}
Fully qualified path: core::result::ResultTrait::map
fn map<U, F, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: U]>(
self: Result<T, E>, f: F,
) -> Result<U, E>
map_or
Returns the provided default (if Err
), or applies a function to the contained value (if Ok
). # Examples
let x: Result<_, ByteArray> = Ok("foo");
assert!(x.map_or(42, |v: ByteArray| v.len()) == 3);
let x: Result<_, ByteArray> = Err("bar");
assert!(x.map_or(42, |v: ByteArray| v.len()) == 42);
Fully qualified path: core::result::ResultTrait::map_or
fn map_or<U, F, +Destruct<E>, +Destruct<U>, +Drop<F>, +core::ops::FnOnce<F, (T,)>[Output: U]>(
self: Result<T, E>, default: U, f: F,
) -> U
map_or_else
Maps a Result<T, E>
to U
by applying fallback function default
to a contained Err
value, or function f
to a contained Ok
value.This function can be used to unpack a successful result while handling an error. # Examples
let k = 21;
let x: Result<ByteArray, _> = Ok("foo");
assert!(x.map_or_else(|_e: ByteArray| k * 2, |v: ByteArray| v.len()) == 3);
let x: Result<_, ByteArray> = Err("bar");
assert!(x.map_or_else(|_e: ByteArray| k * 2, |v: ByteArray| v.len()) == 42);
Fully qualified path: core::result::ResultTrait::map_or_else
fn map_or_else<
U,
D,
F,
+Drop<D>,
+Drop<F>,
+core::ops::FnOnce<D, (E,)>[Output: U],
+core::ops::FnOnce<F, (T,)>[Output: U],
>(
self: Result<T, E>, default: D, f: F,
) -> U
map_err
Maps a Result<T, E>
to Result<T, F>
by applying a function to a contained Err
value, leaving an Ok
value untouched.This function can be used to pass through a successful result while handling an error. # Examples
let stringify = |x: u32| -> ByteArray { format!("error code: {x}") };
let x: Result<u32, u32> = Ok(2);
assert!(x.map_err(stringify) == Result::<u32, ByteArray>::Ok(2));
let x: Result<u32, u32> = Err(13);
assert!(x.map_err(stringify) == Err("error code: 13"));
Fully qualified path: core::result::ResultTrait::map_err
fn map_err<F, O, +Drop<O>, +core::ops::FnOnce<O, (E,)>[Output: F]>(
self: Result<T, E>, op: O,
) -> Result<T, F>