36
you are viewing a single comment's thread
view the rest of the comments
[-] taladar@sh.itjust.works 2 points 2 years ago

There are a few very questionable things in there. Unwrap should literally never appear in production code so if your code base uses it so often that you want a short-hand syntax for it that calls into doubt everything else you wrote.

[-] cbarrick@lemmy.world 3 points 2 years ago

Unwrap should literally never appear in production code

Unwrap comes up all the time in the standard library.

For example, if you know you're popping from a non-empty vector, unwrap is totally the right too for the job. There are tons of circumstances where you know at higher levels that edge cases defended against at lower levels with Option cannot occur.

[-] BB_C@programming.dev 5 points 2 years ago* (last edited 2 years ago)

(DISCLAIMER: I haven't read the post yet.)

For example, if you know you’re popping from a non-empty vector, unwrap is totally the right too(l) for the job.

That would/should be .expect(). You register your assumption once, at the source level, and at the panic level if the assumption ever gets broken. And it's not necessarily a (local) logical error that may cause this. It could be a logical error somewhere else, or a broken running environment where sound logic is broken by hardware or external system issues.

If you would be writing comments around your .unwrap()s anyway (which you should be), then .expect() is a strictly superior choice.

One could say .unwrap() was a mistake. It's not even that short of a shortcut (typing wise). And the maximumly lazy could have always written .expect("") instead anyway.

[-] livingcoder@programming.dev 0 points 2 years ago

I personally think that unwrap and the question mark operator were a mistake.

load more comments (6 replies)
load more comments (7 replies)
load more comments (8 replies)
this post was submitted on 24 Jun 2024
36 points (90.9% liked)

Rust

8300 readers
24 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 3 years ago
MODERATORS