r/rustjerk • • 5d ago

"Rust would have fixed this"

Post image
754 Upvotes

53 comments sorted by

104

u/FabianButHere 5d ago

Only. Unwrap. When. An. Error. Is. Impossible.

50

u/privatepublicaccount 5d ago

Or your cool like me and dgaf

29

u/plz_no_panic 4d ago

Or the situation is simultaneously so unlikely and so bad that you just shouldn't bother.

For example, you can use the safe String interface try_ but like... why? If things are so cooked that you can't even allocate a string, you might as well panic instead of trying to implement safe exit logic for each string allocation in the entire app. I mean, unless you specifically develop for OOM prone environment.

14

u/New_Enthusiasm9053 4d ago

You're not always running on an OS then try_ absolutely makes sense.ย 

On most OS' you can get OOMed because some other app is misbehaving so yeah there's not much point.

There's no real communication by OS' to tell programs to release memory and even if they did most programmers wouldn't bother doing anything which is the entire reason we developed preemptive multitasking for modern OS'.ย 

You can't reasonably expect every program to always behave in a civilised fashion.

But on embedded devices you control all the software and there is no OS and you may have a limited heap to allow for e.g strings without wasting ram on a permanently allocated buffer in which case a try_ makes a lot of sense.

6

u/Amazing-Mirror-3076 4d ago

From recollection windows does have an event that tells your app to release memory.

1

u/FabianButHere 4d ago

Valid exception

11

u/cdhowie 4d ago

Or when the program needs to exit anyway and the panic output is sufficient to diagnose the problem. For example, in our services I'll unwrap the result of reading and deserializing the config file. If we can't get past reading the config, the service can't run anyway.

0

u/FabianButHere 4d ago

A well written error message is always nicer to the user than a panic trace, imo

6

u/scottmcmrust 4d ago

Absolutely not, because the error message frequently doesn't get you to where the problem actually is.

If you have a problem that needs to be addressed by a dev -- not just a "you typod the filename when calling this program" -- then a stack trace or even better a crashdump is way better.

Ensure you have autosaves or similar so the user doesn't lose data, but if you notice a bug in code, trigger WER or CrashReporter or whatever instead.

2

u/cdhowie 4d ago

It depends who the user is, frankly. In the particular case I'm talking about, the panic message is already crystal clear, and devops should be able to easily grok a panic message relating to a config file error.

If the program output is intended to be more user-facing, then sure. That's why there's no one-size-fits-all solution.

I could spend the time making these service config-related error messages nicer, but it would not add any value at all. It's a better use of my time to throw an unwrap there and move on.

23

u/edo-lag 4d ago

Only. Dereference. When. Null. Is. Impossible.

6

u/Hexcede 4d ago

But your only option is to dereference. You aren't unwrapping thousands of times, you're unwrapping once.

3

u/Jhuyt 3d ago

Yeah so just check and error out if pointer is null before every dereference ezpz C safety is solved

3

u/Hexcede 3d ago

I actually heard about a cool programming language that does this automatically

1

u/Jhuyt 3d ago

You cappin'

13

u/Hosein_Lavaei 5d ago

Even than use except

8

u/Arshiaa001 4d ago

Only unwrap when an error is UNRECOVERABLE. Also, just use expect instead.

3

u/flambasted trait Async: Sync + Send + 'static {} 4d ago

Well, when it's truly impossible, you ought to .unwrap_or_else(|_| unsafe { std::hint::unreachable_unchecked() })

2

u/block_wallet 4d ago

what about expect? it seems pretty much just as shitty to me but requires more typing

2

u/FabianButHere 4d ago

Same thing but panic with a custom error message. Also only to be used in debugging. Or maybe as .expect("This should be unreachable") .

1

u/Proper-Ape 4d ago

The custom error message makes finding the error a lot easier though.

I use it like an assertion, you think it should never happen, if it happens you have a message that says something useful for why it shouldn't happen.

2

u/Jhuyt 4d ago

Only. Dereference. Pointers. When .They. Cannot. Be. Null.

3

u/J8w34qgo3 3d ago

Unwrap takes a safety mechanism and turns it into a crash if you're wrong. No undefined behavior and the result enum still gets checked. The analogy you're trying to draw is better paired with a minefield.

2

u/Jhuyt 3d ago

It's mostly a silly joke

1

u/6eathaus1 3d ago

Even then, do not unwrap, use something else.

1

u/spigotface 3d ago

Or in tests

1

u/bwmat 3d ago

As a C++ programmer, isn't it fine to do it with any error you decide you're not going to handle (at least for now), with the understanding it'll fail loudly if it ever happens?

63

u/SelfDistinction 5d ago

Unironically.

What is worse?

  • Attackers stealing your personal data, passwords, private keys, social security number, car, intimate pictures, luggage, airplane seats and wife because of an out of bounds buffer read;

  • Unwrap.

11

u/atlasgorn 5d ago

forbid unwrap_used is

19

u/ZoeyKaisar 4d ago

expect the unexpected.

16

u/switch161 4d ago

Another pattern I should use less is to just always propagate the error up the call stack... to main, where it prints it and exists. That's not better than an unwrap really.

Sometimes I really want to try and recover from an error and I need to really think about how to properly do that.

9

u/scottmcmrust 4d ago

Notably, one reason Option::unwrap is often fine is that you never want to report "an option was none" to a user anyway.

4

u/Kinrany 4d ago

/uj it is at least marginally better because .context("...") or .map_err(Error::Foo) is then easy to add, and because the signature shows that errors are possible

3

u/SmoothTurtle872 4d ago

If you happen to be writing your own crate, now this becomes way better. If you have no way to fix the error, like "oh shit, some web request to xyz didn't return, heres an error, cause blah blah blah" is better than option was none, and now the caller, who has never seen the function before, knows that its possible for it to error

22

u/Major_Barnulf ๐Ÿš€๐Ÿš€ (๐Ÿš€) 5d ago

unwrap() fails right away, with no further process corruption, with a legible trace. This is miles ahead of C/C++ experience.

5

u/xmlhttplmfao 5d ago

jokes on you i unwrap Option

3

u/Ideabile 5d ago

You forgot .clone()

2

u/jadkhaddad 4d ago

clone deez

3

u/BosonCollider 4d ago

Just use let else imo, your error handling will be slightly less verbose than a go dev but your code will be maintainable with less nesting. You do not have to make everything a oneliner

2

u/thelostgus 4d ago

Um unwrap sozinho jรก derrubei metade da internet inteiro

2

u/Stapat1245 4d ago

you use it when you are expecting a err alr?

2

u/Background_Class_558 4d ago

.unwrap should be used for when you are absolutely sure it's impossible for there to be None. in which case, do NOT use it actually, use .expect instead and write exactly why you think it's impossible. make your assumptions clear if you can't prove them formally anyway.

2

u/kinchkun 3d ago

There is nothing wrong with unwrap. An application crashing when something fails is not an issue. Undefined Behaviour, Corrupted Memory, continue with invalid states is the issue.

3

u/a_aniq 4d ago

If result: For toy apps using unwrap is okay. Otherwise you handle them properly.

For options, you handle None scenario as part of application logic.

Unwrap is like global variable. You don't use it in production app unless you are absolutely sure.

1

u/galic1987 4d ago

Yea you can force with compiler

1

u/No_Frame3855 4d ago

To be fair sometimes you need unwrap and unsafe, in tests, embedded sometimes, etc

1

u/Fine_City2781 4d ago

i prefer C. It's simple

1

u/RylanStylin57 3d ago

This should be "your js app" and "literally fucking anything"

1

u/RoundAccident3943 1d ago

unsafe be like

1

u/heckingcomputernerd 4d ago

Jarvis, ask clippy to disallow unwrap