News for hackers. Stuff that matters.
Re:Rust should not have provided `unwrap` by Cyph0n on Friday July 15, 2022 @01:13PM (#32110445)
It’s still useful in cases where you’ve already handled the error case. For example:

  if res.is_err() {
    // Do something
    return;
  }

  let res = res.unwrap();
Re:Rust should not have provided `unwrap` by JoshMcguigan on Friday July 15, 2022 @01:15PM (#32110467)
You can replace this with a match statement to avoid the unwrap.
Re:Rust should not have provided `unwrap` by Macha on Friday July 15, 2022 @01:18PM (#32110513)
It does introduce another layer of nesting in the code, which can be undesirable from a readability perspective at times
Re:Rust should not have provided `unwrap` by kannanvijayan on Friday July 15, 2022 @01:36PM (#32110744)
Not necessarily, you can extract the inner type by using the match as an expression, e.g.:

``` let val = match res { Err(e) => { ... return ...; } Ok(v) => v, }; // Use val without unwrapping here. ```

That said, even though I'd avoid unwrap in this particular case in my code, I generally disagree with the author.

Taking out unwrap and leaving in expect would simply lead to a bunch of code that goes `let val = result.expect("!");`, which is equivalently bad when held up against the criticisms the author makes of unwrap.

Unwrap isn't really harmful so much as a symptom and an escape valve around the fact that our type systems really aren't powerful enough to derive all the invariants the programmer can about their code.

Re:Rust should not have provided `unwrap` by johntb86 on Friday July 15, 2022 @01:29PM (#32110665)
Rust is getting let-else to solve that problem: https://rust-lang.github.io/rfcs/3137-let-else.html
Re:Rust should not have provided `unwrap` by Macha on Friday July 15, 2022 @01:37PM (#32110756)
Ahh, looks nice, here's hoping it gets stabilized
Re:Rust should not have provided `unwrap` by masklinn on Friday July 15, 2022 @01:59PM (#32111005)
Fwiw there’s a small crate called guard which has provided this feature for several years.

I heartily endorse it.

Re:Rust should not have provided `unwrap` by Cyph0n on Friday July 15, 2022 @01:49PM (#32110889)
Wow, this would definitely make it much cleaner!
Re:Rust should not have provided `unwrap` by Cyph0n on Friday July 15, 2022 @01:20PM (#32110538)
Of course, but I think this approach signals intent a bit more clearly: we want to get error handling out of the way and then just proceed with the happy path. It’s also a tad less verbose.
Re:Rust should not have provided `unwrap` by mrits on Friday July 15, 2022 @01:18PM (#32110519)
I'd use a 'if let' for something so short.
Re:Rust should not have provided `unwrap` by throwaway234232 on Friday July 15, 2022 @02:41PM (#32111432)
This is a good use case for let-else, however I won't agree with you here on usage of unwrap for the sake of brevity. I will opt for `?` or match instead.