An unwrap! macro crate


#1

Here’s a crate I wrote which I think is very handy, especially for tests and examples: https://github.com/canndrew/unwrap

Please tell me what you think.


Is there a clean way to merge Option and Result in a macro
#2

Cool, that’s really helpful, thanks!
I’ll try it out in one of my projects.

I use unwrap() quite often when testing things, after that I turn them into matches. Ideally stable code shouldn’t have any unwrap() in it.


#3

I use unwrap() quite often when testing things, after that I turn them into matches. Ideally stable code shouldn’t have any unwrap() in it.

For the most part, I agree. There are couple of cases where it makes sense to use it in stable code though, for example when you want to propagate a panic by unwrapping the result of Mutex::lock or JoinHandle::join. Also rust’s type system isn’t perfect so sometimes it’s possible to “know” that a certain condition can’t occur yet there’s no way to prove it within the type system. An example: you put a value in an Option and then immediately want to get a reference to that value.