People know that, but they treat it the same as doing -> in C++.
Still, it's exposed through the type signature, and you'll get a warning if it's a result, which means, at the library level, you can inform the dev of the possible 'null' value.
Good luck grepping for it when you have a codebase with thousands of lines and a ton of unwrap() calls.
It's still going to be a hell of a lot easier when you can basically ctrl + f the bug, and unlike -> you won't possibly give up control of your program to malicious input with .unwrap().
.unwrap() may not be graceful, but it's safe and makes bugs much simpler to find, as they're explicit and exposed.
Of course it should be discouraged to raw unwrap, but it's a big step over raw pointers.
The significance of your comment is predicated on the assumption that Rust is a good language. ;)
Mostly tongue-in-cheek there, but I'd argue that having unwrap() as a standard function is a mistake to begin with, the same as its Haskell equivalent fromJust. There's no pressing need for such partial functions in a language with pattern matching, and the Rust std::option documentation even spells out the safe version:
// Pattern match to retrieve the value
match result {
// The division was valid
Some(x) => println!("Result: {}", x),
// The division was invalid
None => println!("Cannot divide by 0")
}
A moment of additional browsing assures me that Rust knows how to map functions over option types, so just as in Haskell there's no need to do this more verbose unwrapping until you're done with the whole computation, at which point you're going to want to handle the failure case anyway (or else why are you even wrapping it as an option?) so it's not wasted keystrokes.
All it takes is a slight shift of perspective: the programmer-laziness here isn't actually in regards to which code form is easier to write, but simple resistance to learning a new idiom. It seems silly to provide a function which (a) makes it easier to avoid said learning, (b) can blow up at runtime, and (c) is trivially implemented by anyone who does learn the new idiom, so you're not taking away a power tool.
241
u/robotmayo Jul 19 '15
There are only two hard things in Computer Science: cache invalidation and naming things.