Why should I prefer `Option::ok_or_else` instead of `Option::ok_or`?

Viewed 13048

I just saw the following change in a pull request:

- .ok_or(Error::new(ErrorKind::Other, "Decode error"));
+ .ok_or_else(|| Error::new(ErrorKind::Other, "Decode error"));

The only differences I know are:

  1. In ok_or we have already created Error by Error::new and passed it into a adaptor.
  2. In ok_or_else we have passed a closure which will produce such a value but it may not be called if there is Some data in the Option.

Did I miss anything?

3 Answers
Related