Is there a canonical way to convert Option<Result<T>>
to Result<Option<T>>
?
fn optres_to_resopt<T>(optres: Option<Result<T>>) -> Result<Option<T>> {
if let Some(res) = optres {
match res {
Ok(v) => Ok(Some(v)),
Err(e) => Err(e),
}
} else {
Ok(None)
}
}
There is Option::transpose:
fn optres_to_resopt<T>(optres: Option<Result<T>>) -> Result<Option<T>> {
optres.transpose()
}
And BTW, the implementation is basically your function:
pub fn transpose(self) -> Result<Option<T>, E> {
match self {
Some(Ok(x)) => Ok(Some(x)),
Some(Err(e)) => Err(e),
None => Ok(None),
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With