pub trait OptionExt {
type Item;
// Required methods
fn err_or<O>(self, ok: O) -> Result<O, Self::Item>;
fn err_or_else<O, F>(self, ok: F) -> Result<O, Self::Item>
where F: FnOnce() -> O;
}
Expand description
Extension trait for Option
.
Required Associated Types§
Required Methods§
Sourcefn err_or<O>(self, ok: O) -> Result<O, Self::Item>
fn err_or<O>(self, ok: O) -> Result<O, Self::Item>
Transforms the Option<T>
into a Result<O, T>
, mapping Some(v)
to Err(v)
and None
to Ok(ok)
.
Arguments passed to err_or
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use
err_or_else
, which is lazily evaluated.
§Examples
use err_or::OptionExt;
let x = Some("foo");
assert_eq!(x.err_or(0), Err("foo"));
let x: Option<&str> = None;
assert_eq!(x.err_or(0), Ok(0));
Sourcefn err_or_else<O, F>(self, ok: F) -> Result<O, Self::Item>where
F: FnOnce() -> O,
fn err_or_else<O, F>(self, ok: F) -> Result<O, Self::Item>where
F: FnOnce() -> O,
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.