| ▲ | echelon an hour ago | ||||||||||||||||
The language semantics do not surface `unwrap` usage or make any guarantees. It should be limited to use in `unsafe` blocks. > There's lots of useful code where `unwrap()` makes sense. On my team, we first try to avoid it (and there are many patterns where you can do this). But when you can't, we leave a comment explaining why it's safe. I would prefer the boiler plate of a match / if-else / if let, etc. to call attention to it. If you absolutely must explicitly panic. Or better - just return an error Result. It doesn't matter how smart your engineers are. A bad unwrap can sneak in through refactors, changing business logic, changing preconditions, new data, etc. | |||||||||||||||||
| ▲ | aw1621107 an hour ago | parent | next [-] | ||||||||||||||||
> It should be limited to use in `unsafe` blocks. That would be a fairly significant expansion of what `unsafe` means in Rust, to put it lightly. Not to mention that I think doing so would not really accomplish anything; marking unwrap() `unsafe` would not "surface `unwrap` usage" or "make any guarantees", as it's perfectly fine for safe functions to contain `unsafe` blocks with zero indication of such in the function signature and. | |||||||||||||||||
| |||||||||||||||||
| ▲ | bigstrat2003 an hour ago | parent | prev [-] | ||||||||||||||||
Restricting unwrap to unsafe blocks adds negative value to the language. It won't prevent unwrap mistakes (people who play fast and loose with it today will just switch to "foo = unsafe { bar.unwrap() };" instead). And it'll muddy the purpose of unsafe by adding in a use that has nothing to do with memory safety. It's not a good idea. | |||||||||||||||||
| |||||||||||||||||