[][src]Trait err_or::OptionExt

pub trait OptionExt {
    type Item;
    pub fn err_or<O>(self, ok: O) -> Result<O, Self::Item>;
pub fn err_or_else<O, F>(self, ok: F) -> Result<O, Self::Item>
    where
        F: FnOnce() -> O
; }

Extension trait for Option.

Associated Types

type Item[src]

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

Loading content...

Required methods

pub fn err_or<O>(self, ok: O) -> Result<O, Self::Item>[src]

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

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

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));
Loading content...

Implementations on Foreign Types

impl<T> OptionExt for Option<T>[src]

type Item = T

Loading content...

Implementors

Loading content...