I realized today that there is no method of Option<T> which would either lift it into a Result<T, E> by converting Some(t) to Ok(t) or return another Result<T, E> that was passed to it.
Something like
_(&self, other: Result<T, E>) -> Result<T, E> {
match self {
Some(t) => Ok(t),
None => other,
}
}
and equivalently for the case where other is a function (i.e. the *_else variants).
This would be useful in cases where you're expecting that a given option is actually a value that is present, but in case it is None, you'd just like to pass the control flow to another result.
When thinking about what name this would be given, probably the most natural choice would actually be ok_or. In fact, thinking about it, it seems that ok_or is unnecessarily restrictive by requiring you to pass the E of Result<T, E> instead of the result itself. The current behaviour of ok_or would then be recovered by simply doing x.ok_or(Err(e)) instead of x.ok_or(e).
This has the additional benefit in that it makes the ok_or of Option parallel the ok of Result more closely. In fact, such an ok_or would behave like a lift into a result (for the happy path), followed by the usual ok of Result, allowing for chaining such as:
opt.ok_or(res1).or(res2).or(res3)
Why was ok_or designed like this? Was it just an oversight? Did I miss some method?
I'm also wondering what the best way is to achieve the functionality of the last snippet in today's Rust. The best I could come up with is
opt.map_or(res1, |v| Ok(v)).or(res2).or(res3)