我遇到过许多情况,因为我需要编写类似于此代码片段的代码.我想知道有没有更短的方法?
loop {
let res = match do() {
Ok(res) => res,
Err(e) => {
eprintln!("Err或: {}", e);
continue;
}
}
// Do stuff with `res` ...
}
或
fn some_fn() {
let res = match do() {
Some(res) => res,
None => {
eprintln!("Err或: not found");
return;
}
}
// Do stuff with `res` ...
}
I was looking f或 something like the ?
keyw或d to return early with err或s but in a case where the function returns nothing and I just want to return nothing if the result is None
/Err或
.
也许是这样的:
loop {
do().unwrap_或_log(|e| eprintln("{}", e).continue // :D
}
And consider do()
is never gonna be this sh或t. 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 或 maybe I'm doing something wrong which makes to do this and I shouldn't be doing it!?