8 ms·
> If a library panics on invalid user data, that's a pretty serious bug. I swear, sometimes it seems like Rust people are from another planet. What do you thin
by svnpenn 4y ago
> If a library panics on invalid user data, that's a pretty serious bug.
I swear, sometimes it seems like Rust people are from another planet. What do you think "unwrap" does? It's not used in every library, but certainly in many of them.
- justinpombrio 4y ago> What do you think "unwrap" does? It panics just like my `my_vector[2]` example does. What did you think `my_vector[2]` did? Libraries use `my_vector[2]` too. I don't get why we're changing topics from one commonly used panicking operation to another.
- deleted 4y ago[deleted]
- nulld3v 4y agoThe assertion remains true though. Unwrap should only be used if you are prototyping or you are 100% sure it will never actually panic. It's just like the IndexOutOfBounds exception in Java. Many functions can theoretically throw it, but most libraries and programs do not catch it because usually if it is thrown it means that something happened that the programmer did not expect and therefore the program should crash.
- elbear 4y agoThere's a difference between what you should do and what people actually do. If a lot of them use unwrap in production, then OP's argument is valid.
- nulld3v 4y agoThe problem would not be that it is commonly used, the problem would be that it is abused. And I don't see that happening currently. The assertion that "If a library panics on user data, that's a pretty serious bug" remains true. If a library is panicking on invalid user data, it is because they are abusing panic, which is a serious bug. Or they just didn't realize that their code could panic, which is also a serious bug.
- deleted 4y ago[deleted]
- nindalf 4y ago> seems like Rust people are from another planet. There’s no need to talk in a condescending manner. What they said was correct - an “unwrap” outside of test/prototyping is considered a serious bug. They Rust loving strawmen you’re creating never claimed that every line of Rust ever written is perfect and bug free.
- Nathanba 4y agoSo you essentially want me to write error handling code nonstop, constantly, all across my functions. Practically after every 5 lines of code there is going to be an unwrap() where I'm not allowed to call unwrap() so I have to know the details of the implementation, the error code, deal with the error code, return early from the function and then gracefully handle it all. Meanwhile in a language that has exceptions I just put a try catch around all the code I think works fine but maybe not and I deal with it in a single location in a way where I dont have to care about what the precise error code might be. Error code programming really seems to be objectively worse for everyone except the compiler writer. Somehow people let themselves get convinced that this is better when it's objectively not.
- Gwypaas 4y agoOr simply use Thiserror together with bubbling. https://crates.io/crates/thiserror/1.0.24 https://crates.io/crates/thiserror/1.0.24
- svnpenn 4y agoSorry, but if the language cant even get errors right, I'm certainly not going to download a package to do it.
- nindalf 4y ago? Are you mistaking Rust for some other language? Error handling in Rust is mostly just using the `?` operator. The Rust book explains - https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator https://doc.rust-lang.org/book/ch09-02-recoverable-errors-wi...
- varajelle 4y agoUnwrap is supposed to be used when the developer know that the error can't happen. Or that if that error happen there is no recovery anyway, and the best thing to do is to abort the program.