I've observed several times a situation where I might want to act based on the ultimate result of a bunch of operations, where I don't care which operation threw the error, just that one of them did, or none of them did.
I can use the ?
operator for this, but it then propagates the error to the function call. I hoped that something like this would work:
fn do_something() {
let i = {
let w = function1()?;
let x = function2(z)?.function3()?;
x.y()?
};
if let Ok(item) = i {
// ..
} else {
// call to some other function or something..
}
// some more code here..
}
However, this doesn't work. Is there some alternate way to achieve this pattern? I know I can wrap the scope in a closure, but this feels ugly and unidiomatic especially if such behaviour is required often. This might be indicative that I need to break the function into more smaller functions, however that proves unwieldy in situations like
let i = {
function1()?.function2()?.function3()
}
where making an entirely new function doesn't quite make sense. Maybe this isn't even the idiomatic way to do things. If so, how should I approach this issue?