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

#1

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(()))
#2

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

2 Likes
#3

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(())

#4

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

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

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())

#6

This is

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

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: