unwrap_or
followed by a function call RS-W1031Calls to unwrap_or
followed by a function call should be replaced with
unwrap_or_else
or unwrap_or_default
, to increase laziness. The argument to
unwrap_or
is evaluated eagerly and unconditionally. unwrap_or_else
or
unwrap_or_default
are evaluated lazily and only if required, sometimes
leading to lower overhead and fewer allocations.
Replace the call to unwrap_or
with unwrap_or_else
or unwrap_or_default
.
// allocates a new `Foo` instance even if `bar` is `Ok(_)`
bar.unwrap_or(Foo::new());
// only allocates if `bar` evaluates to `Err(_)`
bar.unwrap_or_else(|| Foo::new());