DieWith

Trait DieWith 

Source
pub trait DieWith<T, E> {
    // Required method
    fn die_with<X: PrintExit, F: FnOnce(E) -> X>(self, func: F) -> T;
}
Expand description

DieWith is a trait implemented on Result only to make exiting with messages and codes easy

Required Methods§

Source

fn die_with<X: PrintExit, F: FnOnce(E) -> X>(self, func: F) -> T

Unwraps a Result, yielding the content of an Ok.

§Exits

Calls process::exit(exit_code) if the value is an Err, after printing the message produced by the given function to stderr.

§Examples

Basic usage:

let x: Result<u32, &str> = Err("emergency failure");
x.die_with(|err| format!("strange: {}", err)); // prints `strange: emergency failure` to stderr then exits with code 1
let x: Result<u32, &str> = Err("emergency failure");
x.die_with(|err| (format!("strange: {}", err), 3)); // prints `strange: emergency failure` to stderr then exits with code 3

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, E> DieWith<T, E> for Result<T, E>

Source§

fn die_with<X: PrintExit, F: FnOnce(E) -> X>(self, func: F) -> T

Implementors§