2 ms·
To be more accurate, unwrap can be a legitimate means of error handling when used in an application, as opposed to a library. But if you're writing a library, t
by kibwen 10y ago
To be more accurate, unwrap can be a legitimate means of error handling when used in an application, as opposed to a library. But if you're writing a library, then unwrapping rather than using Result is a surefire way to make your users hate you. :)
- wyldfire 10y agoAh! I stand corrected. "library" is a pretty sane use case for barring unwrap(), one that cargo knows is the current goal. I still don't think it's general enough but maybe it's worth a warning. Aside: CPython's gdbm support is provided by libgdbm that calls exit() for you if if finds something it's not happy with (corrupted database, e.g.). O.o
- Nemo157 10y agoSometimes you've proven some invariant in some other way, so you know that unwrapping is guaranteed to not panic. Although in those cases I prefer to use .expect("this will not fail because of blah") instead in the spirit of self-documenting code.
- kibwen 10y agoAgreed, I use `.expect("Infallible")` for such cases.
- staticassertion 10y agoEven in a library there are valid use cases for an unwrap. If you can literally guarantee that the value is present it's fine. In libraries one should be wary about it, and never use it if the unwrap might actually panic, but otherwise you're good to go.