Can anyone tell me why #rust #rustlang '.unwrap()' is bad? I understand "because it panics" but that's too superficial an answer. AFAICT, every occurrence is by choice of dev. So either it's a bug by dev, or mistake by user. So does '.unwrap()' get crap only bc ppl don't understand the use cases?

Reply to this note

Please Login to reply.

Discussion

It's better pratice to always handle it with _or...() and in worst case, return an error using a Result<>. It's so easy that there's no legitimate reason no to.

Proper errors beat panics.

Using `.unwrap()` is not "bad," per se. It is just frequently misused. Utilizing something like `match` or `if let` provides a more idomatic way of handling both expected and unexpected error-cases without causing your application to come to a grinding halt. One such example includes needing it to exit, but wanting to clean up artifacts. Another is creating and handling abstracted error types. Regardless, a good rule of thumb is only to use `.unwrap()` when you anticipate the desired failure outcome should be a panic.