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§
Sourcefn unwrap_display(self) -> Twhere
E: Display,
fn unwrap_display(self) -> Twhere
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!`
Sourcefn expect_display(self, msg: &str) -> Twhere
E: Display,
fn expect_display(self, msg: &str) -> Twhere
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::Display
ed Err
.
§Examples
let x: Result<u32, _> = Err(io::Error::other("oh no!"));
x.expect_display("Testing expect"); // panics with `Testing expect: oh no!`
§Recommended Message Style
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.