Convert any result variant to `Ok(())`

I want to map any result (Err or OK) to an Ok(()). This is my current approach.
Is there a cleaner/idimoatic way to do this?

do_something().map_err(|err| notify(&err).and_then(|_| Ok(())).or_else(|_| Ok(()))

let _ = do_something(); Ok(())

2 Likes

I should clarify on an Err I first need to do notify(&err)
and also don't want the warning from "unused result" if I do: do_something().map_err(|err| notify(&err); Ok(())

You can still map_err for that, bound to _, or something like:

if let Err(err) = do_something() {
   let _ = notify(&err);
}
Ok(())
1 Like

Ah! thanks that's perfect. I guess I got caught up in trying to chain the calls...
Could be cool to have something like: do_something().map_err(|err| notify(&err).finally(OK())

This is

do_something().map_err(|err| notify(&err)).map_or_else(|_| OK(()), |_| Ok(()));

Applying a function would have been easy

let _ = do_something().map_err(|err| notify(&err));
Ok(())

I mainly came back though to point out let _ = ... does not trigger an unused variable warning. Can be useful at times :slight_smile: