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));
Iterator::map is materially different because it’s lazy, but it should be totally fine for you to use Option::map and drop the return value, which might just be Option<()>.
The trick here, which is easy to forget, is that map does the wrapping of the closure’s U return value as an Option. You don’t have to return a pre-wrapped Option.