I often find myself writing code that, in human language reads like "if the method call is OK, call this function, else, call that function".
In code:
match client.favourite(&status.id) {
Ok(_) => info!("Favourited {}", &status.id),
Err(err) => error!("Could not favourite {}: {:#?}", &status.id, err),
};
Here, I'm merely calling a macro that logs to stdout, which could probably be abstracted away. The question is not whether this exact case can be improved, I'm looking if it is more Rust-ish to write this differently, or whether this is considered The Best Way already.
E.g. I could imagine something like:
client.favourite(&status.id)
.on_ok(|_| info!("Favourited {}", &status.id))
.on_err(|err| error!("Could not favourite {}: {:#?}", &status.id, err))
But I don't see such methods in the std-lib. But maybe I'm reading them wrong, maybe one of the unwrap_or_* does allow such usages?
Am I trying to do something un-rust-ish, and is the match/branch really the common way, or are there methods on Result that I'm overlooking, or usages that I am missing?
You have
mapandmap_errwhich can be used for that:Playground