Seeing the comments under this question, made me wonder - should using unwrap() be considered bad practice?
In my view, sometimes unwrapping makes sense. At least in these two scenarios:
- We know an
Optionvalue cannot beNone, because we have already handled that case, earlier in the code. An example:
if o.is_none() {
// do some stuff here...
return ...;
}
// ...
o.unwrap() // <--- Here I do NOT expect a None
- If we develop a simple short-lived tool, which is simply gonna end on error either way, relying on panics instead of "proper" error handling (with
Result,?, etc.) could simplify code, more or less.
Obviously, sometimes relying on unwrap() might cause more harm than good, so it isn't quite obvious to me whether I should always refrain from using unwrap() or not? If not, when would it be OK to use unwrap()?