Result::expect_pretty that uses Display instead of Debug

The panic message from Result::expect is not really suitable for presenting to a user as the Debug impl will usually lead to a bunch of nested structiness. Would it make sense to add an equivalent that uses Display instead? It would look like this:

impl<T, E: fmt::Display> Result<T, E> {
    pub fn expect_pretty(self, msg: &str) -> T {
        match self {
            Ok(t) => t,
            Err(e) => panic!("{}: {}", msg, e),

The expect method isn’t meant to be used for display to the user; it is meant to be a debugging tool when an invariant in your program is exposed.

I personally wouldn’t want to see expect_pretty in std because I don’t think it has broad utility.

Ah so it's more like a shorthand for assert!(result.is_ok())?

Yes, with the added convenience of doing case analysis and including the Err variant in the panic message.

In general, panic messages are themselves a debugging tool. If an end user of your Rust program sees a panic, it probably should be considered a bug.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.