Rust

Rust

Made by DeepSource

Found unnecessary closure RS-W1202

Anti-pattern
Minor
Autofix

The or_else family of functions such as Result::map_or_else and Option::map_or_else accept a closure as an argument as a means of delaying the "else" computation. However, simple closures such as || 5 add extra overhead.

Consider using the "eager" variants of these functions instead, as it reduces laziness and improves readability:

  • map_or_else with map_or
  • unwrap_or_else with unwrap_or
  • ok_or_else with ok_or

Bad practice

let x = Err(2);
x.unwrap_or_else(|| 0);

Recommended

let x = Err(2);
x.unwrap_or(0);