Trait OptionExt

Source
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§

Source

type Item

Value type, i.e. T for Option<T>.

Required Methods§

Source

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));
Source

fn err_or_else<O, F>(self, ok: F) -> Result<O, Self::Item>
where F: FnOnce() -> O,

Transforms the Option<T> into a Result<O, T>, mapping Some(v) to Err(v) and None to Ok(ok()).

§Examples
use err_or::OptionExt;

let x = Some("foo");
assert_eq!(x.err_or_else(|| 0), Err("foo"));

let x: Option<&str> = None;
assert_eq!(x.err_or_else(|| 0), Ok(0));

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.

Implementations on Foreign Types§

Source§

impl<T> OptionExt for Option<T>

Source§

type Item = T

Source§

fn err_or<O>(self, ok: O) -> Result<O, Self::Item>

Source§

fn err_or_else<O, F>(self, ok: F) -> Result<O, Self::Item>
where F: FnOnce() -> O,

Implementors§