this post was submitted on 01 Aug 2025
170 points (97.2% liked)

Programmer Humor

26046 readers
957 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

Don't say anyway, say anyhow

you are viewing a single comment's thread
view the rest of the comments
[–] magic_lobster_party@fedia.io 38 points 1 month ago (3 children)

Unwrap means it forces to evaluate the result as an ”ok value”. If it’s an ”error value”, it will crash. It’s a bad practice to rely on it, as it’s one of the most common ways a Rust programs can crash.

Rust offers many options to handle errors that don’t risk crashing. For example, unwrap_or_default, which means ”if it’s an error value, use the default value for this type, such as 0 for integers”

[–] Korne127@lemmy.world 16 points 1 month ago (2 children)

I mean using unwrap is not bad practice if the value is guaranteed to not be none, which can happen frequently in some applications.

[–] qaz@lemmy.world 10 points 1 month ago* (last edited 4 weeks ago)

A good example would be regex. After validating it when writing the program it should always compile, although this could also be solved with a proc macro that validates the regex at compile time.

[–] mobotsar@sh.itjust.works 4 points 4 weeks ago* (last edited 4 weeks ago) (3 children)

If it's guaranteed to not be None, why is it an Option?

[–] emilgardis@lemmy.ml 3 points 4 weeks ago

Here's a bad example but hopefully captures the why. https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=43d055381e7bb52569c339d4526818f4

We have a condition we know must be satisfied (the option will always be Some), but cant prove in code.

[–] marcos@lemmy.world 2 points 4 weeks ago* (last edited 4 weeks ago)

Oh, it can happen when you do calculations with compile-time constants...

But the GP's claim that it's a "frequent" thing is suspect.

(Crashing is also useful when you are writing and-user applications, but you'll probably want .expect like in the meme.)

[–] Korne127@lemmy.world 1 points 3 weeks ago

A very typical use-case would be getting something from a HashMap (or a Vector) and calling unwrap because you know it must exist (as you got a reference to the index or object that must be valid in the HashMap or Vector).
Or if you call a function that returns Option<…> depending on the current state and you know that it must return Some(…) in the current situation.

[–] sirdorius@programming.dev 8 points 4 weeks ago* (last edited 4 weeks ago) (1 children)

Unwrap is good for prototyping and trying out stuff fast, but it generally shouldn't make it past a code review onto main, unless you're very sure

[–] dejected_warp_core@lemmy.world 2 points 3 weeks ago

Exactly.

Personally, I call it "python mode" since you're staying on the "happy path" and let the program just crash out if those expectations aren't met.