I have come across many instances in that I need to write a code similar to this snippet. I was wondering if there's a shorter way to do it?
loop {
let res = match do() {
Ok(res) => res,
Err(e) => {
eprintln!("Error: {}", e);
continue;
}
}
// Do stuff with `res` ...
}
or
fn some_fn() {
let res = match do() {
Some(res) => res,
None => {
eprintln!("Error: not found");
return;
}
}
// Do stuff with `res` ...
}
I was looking for something like the ? keyword to return early with errors but in a case where the function returns nothing and I just want to return nothing if the result is None/Error.
Maybe something like this:
loop {
do().unwrap_or_log(|e| eprintln("{}", e).continue // :D
}
And consider do() is never gonna be this short. It's probably a chain of a few function calls which is already too long.
Maybe the way I'm doing it is the only way or maybe I'm doing something wrong which makes to do this and I shouldn't be doing it!?