ErrOrElse

Trait ErrOrElse 

Source
pub trait ErrOrElse {
    type Err;

    // Required method
    fn err_or_else<OK>(self, ok: impl FnOnce() -> OK) -> Result<OK, Self::Err>;
}
Expand description

The SomeToErrElse trait provides a convenient method to convert an Option<T> into a Result<OK, T> by supplying a closure that generates the OK value for the Result when the input is None.

§Examples

Basic usage:

use some_to_err::ErrOrElse;

let input: Option<&str> = None;
let result = input.err_or_else(|| "OK");
assert_eq!(result, Ok("OK"));
use some_to_err::ErrOrElse;

let input = Some("Error");
let result = input.err_or_else(|| "OK");
assert_eq!(result, Err("Error"));

Required Associated Types§

Required Methods§

Source

fn err_or_else<OK>(self, ok: impl FnOnce() -> OK) -> Result<OK, Self::Err>

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> ErrOrElse for Option<T>

Source§

type Err = T

Source§

fn err_or_else<OK>(self, ok: impl FnOnce() -> OK) -> Result<OK, Self::Err>

Implementors§