I have sometimes found it necessary to do the following:
let x: Option<u32> = foo_bar();
...
if let Some(y) = x {
fn_call_with(y);
}
Some functions on options are provided, are can be very useful, like and_then
or unwrap_or
. However, there is no equivalent to a “flat for_each”. So I propose a new function on Option<T>
called with
of for
which takes a closure that is only called if the option has some value.
Example from above:
let x: Option<u32> = foo_bar();
...
x.with(|y| fn_call_with(y));