Consider this:
loop {
let data = match something() {
Err(err) => {
warn!("An error: {}; skipped.", err);
continue;
},
Ok(x) => x
};
let data2 = match something_else() {
Err(err) => {
warn!("An error: {}; skipped.", err);
continue;
},
Ok(x) => x
};
// and so on
}
If I didn't need to assign the ok-value to data
, I'd use if let Err(err) = something()
, but is there a shortcut to the code above that'd avoid copy-pasting the Err/Ok branches on this, I think, typical scenario? Something like the if let
that would also return the ok-value.
let data
and thecontinue
and then just put "other stuff" inside theOk(x) =>
case. – DiversityResult
, this force code to check the error, if there was something to bypass this, what would be the point ? This construct is so frequent in Rust that?
is used to avoid copy of code. For test only or in main function, you can use.unwrap()
or.expect()
– Brethrenif let
, seeing as that it's effectively seeking an alternative to the language construct. – CountermanResult
s is to use one of the comprehension crates like map_for or mdo. (Note: I'm the author ofmap_for
) – Boccioni