Trait ResultExt

Source
pub trait ResultExt<T, E> {
    // Required methods
    fn unwrap_display(self) -> T
       where E: Display;
    fn expect_display(self, msg: &str) -> T
       where E: Display;
}
Expand description

Extensions for Result<T, E>.

Required Methods§

Source

fn unwrap_display(self) -> T
where E: Display,

Same as Result::unwrap(), but uses fmt::Display instead of fmt::Debug.

Returns the contained Ok value, consuming the self value.

Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the Err case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default.

§Panics

Panics if the value is an Err, with a fmt::Display panic message provided by the Err’s value.

§Examples

Basic usage:

let x: Result<u32, &str> = Ok(2);
assert_eq!(x.unwrap_display(), 2);
let x: Result<u32, _> = Err(io::Error::other("oh no!"));
x.unwrap_display(); // panics with `oh no!`
Source

fn expect_display(self, msg: &str) -> T
where E: Display,

Same as Result::expect(), but uses fmt::Display instead of fmt::Debug.

Returns the contained Ok value, consuming the self value.

Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the Err case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default.

§Panics

Panics if the value is an Err, with a panic message including the passed message, and the fmt::Displayed Err.

§Examples
let x: Result<u32, _> = Err(io::Error::other("oh no!"));
x.expect_display("Testing expect"); // panics with `Testing expect: oh no!`

We recommend that expect messages are used to describe the reason you expect the Result should be Ok.

let path = std::env::var("IMPORTANT_PATH")
    .expect_display("env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`");

Hint: If you’re having trouble remembering how to phrase expect error messages remember to focus on the word “should” as in “env variable should be set by blah” or “the given binary should be available and executable by the current user”.

For more detail on expect message styles and the reasoning behind our recommendation please refer to the section on “Common Message Styles” in the std::error module docs.

Implementations on Foreign Types§

Source§

impl<T, E> ResultExt<T, E> for Result<T, E>

Source§

fn unwrap_display(self) -> T
where E: Display,

Source§

fn expect_display(self, msg: &str) -> T
where E: Display,

Implementors§