this post was submitted on 01 Aug 2025
148 points (96.8% liked)

Programmer Humor

25425 readers
1034 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
[–] somerandomperson@lemmy.dbzer0.com 19 points 1 day ago (2 children)

Sad to be the one that does not get it.

[–] magic_lobster_party@fedia.io 35 points 1 day 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 14 points 1 day 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.

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

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

[–] marcos@lemmy.world 2 points 1 day ago* (last edited 1 day 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.)

[–] qaz@lemmy.world 9 points 1 day ago* (last edited 1 day 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.

[–] sirdorius@programming.dev 6 points 1 day ago* (last edited 1 day ago)

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

[–] Ediacarium@feddit.org 14 points 1 day ago (2 children)

Languages like Java or C++ have Exceptions, which are errors, that are not explicitly mentioned in the function signature. Meaning, you might need to handle an exception you didn't even know existed. And if you don't, your program will just crash when these exceptions occur.

In Rust all errors are explicitly mentioned and part of the return type. Because of this Rust has a lot of ways to quickly handle an error. One of those ways is "Trust me, bro" (or .unwrap()), which converts the combined error/success return type into just a success type, causing the program to crash if it actually was an error, restoring the more unsafe behavior of other languages.

[–] dgriffith@aussie.zone 6 points 1 day ago (1 children)

causing the program to crash if it actually was an error, restoring the more unsafe behavior of other languages.

Wellllll it's more of an abrupt exit rather than a crash, which is still better than eg. silently accessing beyond the end of an array, or ending up with a pointer to nowhere when you thought you had a sane memory reference.

[–] arendjr@programming.dev 4 points 1 day ago (2 children)

“An abrupt exit”, more commonly known as a “crash”.

If you’re going to argue that an exit through panic!() is not a crash, I will argue that your definition of a crash is just an abrupt exit initiated by the OS. In other words, there’s no meaningful distinction as the result is the same.

[–] qaz@lemmy.world 6 points 1 day ago* (last edited 1 day ago) (1 children)

I don't think that's a valid comparison. The behavior does differ when it comes to cleanly releasing resources. Rust's panic performs the drop actions for the current values on the stack, a SIGILL or SIGSEGV crash doesn't.

#[derive(Debug)]
struct MyStruct {}

impl Drop for MyStruct {
	fn drop(&mut self) {
		println!("{:?}", "imagine cleanup here"); // this is called
	}
}

fn main() {
	let a = MyStruct {};
	panic!("panic!");
        println!("{a:?}");
}

Try it yourself

[–] arendjr@programming.dev 7 points 1 day ago

That’s fair, although technically you could catch SIGSEGV and release resources that way too.

Also, given that resources will be reclaimed by the OS regardless of which kind of crash we’re talking about, the effective difference is usually (but not always) negligible.

Either way, no user would consider a panic!() to be not a crash because destructors ran. And most developers don’t either.

[–] dgriffith@aussie.zone 1 points 1 day ago* (last edited 21 hours ago)

I was talking more about unwrap causing a panic rather than calling the actual panic macro directly. Rust forces the programmer to deal with bad or ambiguous results, and what that is exactly is entirely decided by the function you are calling. If a function decides to return None when (system timer mod 2 == 0), then you'd better check for None in your code. Edit: otherwise your code is ending now with a panic, as opposed to your code merrily trotting down the path of undefined behaviour and a segfault or similar later on.

Once you get to a point where we are doing the actual panic, well, that is starting to just be semantics.

[–] thevoidzero@lemmy.world 4 points 1 day ago

Crashing through unwrap is not necessarily restoring unsafe behavior of other languages though. I'd consider this wayyyy better than silently continuing with invalid value until the program tries something that doesn't make sense/is overreaching and it crashes.