Slapping on a `.expect` is also error handling!
-
If it's guaranteed to not be
None
, why is it anOption
?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.
-
If it's guaranteed to not be
None
, why is it anOption
?wrote on last edited by [email protected]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.)
-
“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.wrote on last edited by [email protected]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.
-
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
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.
-
If it's guaranteed to not be
None
, why is it anOption
?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 returnsOption<…>
depending on the current state and you know that it must returnSome(…)
in the current situation.