Let's say I have a function that return a Result
fn do_stuff() -> Result<ResultType, ErrorType> {
}
The implementation of this function is using a struct provided by a third party provider hence I cant really show the exact code, but the thing about this struct is, it provides an iterator and the elements are retrieved as some. so something like this
fn do_stuff() -> Result<ResultType, ErrorType> {
(third_party: ThirdParty).iter().map(|s| {
s // -> is Option<Stuff>
})
}
Now there needs to be certain operation that needs to be performed on s. And these operations can fail so they return a Result
fn do_stuff() -> Result<ResultType, ErrorType> {
(third_party: ThirdParty).iter().map(|s| {
match s {
Some(thing) => {
let output = operate_on_thing(thing)
},
None => {
None
}
}
})
}
The problem now is, if the operation fails, I want the error within the map to bubble up to be the error result returned from the function. That is
fn do_stuff() -> Result<ResultType, ErrorType> {
(third_party: ThirdParty).iter().map(|s| {
match s {
Some(thing) => {
let output = operate_on_thing(thing)
// if output is an Error, then return from this mapping and have that Error value
// be the return of the do_stuff function. if it is not an error then Some s is successfully mapped
},
None => {
None
}
}
})
}
Apologies I can't reproduce with exact code.
Any thoughts on how to go about this?