Theseus icon indicating copy to clipboard operation
Theseus copied to clipboard

Use `unwrap_or_else` instead of `unwrap_or`, and `ok_or_else` instead of `ok_or`

Open kevinaboos opened this issue 7 years ago • 1 comments

The unwrap_or(default) and ok_or(default) functions are used ubiquitously, but they eagerly evaluate the default value, even when the actual value is Some or Ok. That's bad, and really wasteful.

Instead, we should convert them to unwrap_or_else(|| closure) and ok_or_else(|| closure), because the closures that provide the default value are not eagerly evaluated; they are only lazily evaluated if the original value was None or Err.

kevinaboos avatar Jun 22 '18 23:06 kevinaboos

We should also enable the clippy lint option that checks for unwrap_or and ok_or, and recommends using the _else versions instead!

kevinaboos avatar Jun 22 '18 23:06 kevinaboos

Closed by #641

kevinaboos avatar Sep 28 '22 18:09 kevinaboos