3 ms·
This is awesome, but I have an off-topic rust question: Why can't we have some syntactic sugar to get rid of .unwrap()?
by pbarnes_1 10y ago
This is awesome, but I have an off-topic rust question:
Why can't we have some syntactic sugar to get rid of .unwrap()?
- steveklabnik 10y agoFor Result, in nightly, there is sugar, though it's for try!, not unwrap(), which serve very similar purposes. It's not yet clear if said sugar will support Option; that's one of the reasons it's not stable yet. Feel free to post off-topic questions on https://users.rust-lang.org/ https://users.rust-lang.org/ any time!
- jdub 10y agoCheck out the try! macro. https://doc.rust-lang.org/book/error-handling.html#the-try-macro https://doc.rust-lang.org/book/error-handling.html#the-try-m...
- whateveracct 10y agoInstead of looking for "syntactic sugar to get rid of unwrap()" I think the better option would be to prove that the Option isn't None. In my experience in other functional languages with Options, if you can't prove it, you're usually making a mistake somewhere upstream in your code.
- bvinc 10y agoThe try! macro should be used much more often than .unwrap(). And there is a feature that has been implemented to make a ? operator that is roughly equivalent to the try macro. It's currently hidden behind a feature gate named question_mark and it's unclear if or when it will be part of the language.