News for hackers. Stuff that matters.
Re:Using unwrap() in Rust is okay by richardwhiuk on Monday August 08, 2022 @12:24PM (#32387568)
I think the `expect()` bad examples are something of a strawman.

The `.expect()` for regex for example would say what the regex is matching for.

I think it'd be desirable to have a `.unwrap_with_context("Context: {}")`, and the you'd get `Context: Inner Panic Info`.

Re:Using unwrap() in Rust is okay by burntsushi on Monday August 08, 2022 @01:12PM (#32388246)
So you're saying that the 'expect()' message when a regex compilation error occurs should be a translation from a terse domain specific language to bloviating prose? :-)

What 'expect()' message would you write for this regex? https://github.com/BurntSushi/ucd-generate/blob/6d3aae3b8005...

I think 'unwrap()' there is perfectly appropriate.

> I think it'd be desirable to have a `.unwrap_with_context("Context: {}")`, and the you'd get `Context: Inner Panic Info`.

Why?

Re:Using unwrap() in Rust is okay by richardwhiuk on Monday August 08, 2022 @01:42PM (#32388621)
.expect("UnicodeData::from_str regex failed to compile")

With this, even with out backtrace, you can work out what happened.

Without it, you just know that some regex somewhere is invalid.

Re:Using unwrap() in Rust is okay by burntsushi on Monday August 08, 2022 @01:49PM (#32388703)
Have you ever seen a Regex::new(..).unwrap() fail? It sounds like maybe not. It also sounds like you haven't seen an 'unwrap()' fail either.

> Without it, you just know that some regex somewhere is invalid.

That's bologna. As I discuss in the blog post, 'unwrap()' tells you the line number at which it panicked. There's even an example showing exactly this. There's even another example showing what happens when you call 'Regex::new(..).unwrap()' and it fails[1]:

    fn main() {
        regex::Regex::new(r"foo\p{glyph}bar").unwrap();
    }
And running it:

    $ cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.00s
         Running `target/debug/rust-panic`
    thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Syntax(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    regex parse error:
        foo\p{glyph}bar
           ^^^^^^^^^
    error: Unicode property not found
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    )', main.rs:4:36
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrac
So you get the line number (as is standard with any 'unwrap()') and you get the actual error message from the regex. No need to even enable the backtrace. You just don't need 'expect()' here.

[1]: https://blog.burntsushi.net/unwrap/#why-not-use-expect-inste...

Re:Using unwrap() in Rust is okay by richardwhiuk on Tuesday August 09, 2022 @05:40AM (#32395849)
Just to be clear, I agree with everything else you've said, and you've produced some awesome Rust libraries.
Re:Using unwrap() in Rust is okay by burntsushi on Tuesday August 09, 2022 @09:11AM (#32397318)
:-)
Re:Using unwrap() in Rust is okay by xvedejas on Monday August 08, 2022 @01:13PM (#32388264)
When you unwrap/expect on an error, the inner error value is already printed. Are you suggesting something more? Reference the implementation details from the article:

    impl<T, E: std::fmt::Debug> Result<T, E> {
      pub fn unwrap(self) -> T {
        match self {
          Ok(t) => t,
          Err(e) => panic!("called `Result::unwrap()` on an `Err` value: {:?}", e),
        }
      }
    }
Re:Using unwrap() in Rust is okay by vincenv on Monday August 08, 2022 @01:13PM (#32388267)
You can get that with anyhow [1] by calling unwrap after `.with_context`.

[1]: https://docs.rs/anyhow/latest/anyhow/