option<A>, f: A -> Option<B>, ==> Option<B>

I have:

let x: Option<A>
let f: fn A -> Option<B>

I’m looking at https://doc.rust-lang.org/std/option/enum.Option.html andnot seeing a function that generates a (x, f) -> Option<B>. Is there builtin function for this? (I expect “yes”, but can’t find the function.)

If I’m understanding this correctly, you want a Option function that takes a value, and a function that returns a Option using that value?
This is the exact same as just calling the function in the first place.
Unless you meant you wanted a function like so:

fn map<T, U>(a: Option<T>, f: impl Fn(Option<T>) -> Option<U>);

https://doc.rust-lang.org/std/option/enum.Option.html#method.and_then

4 Likes

f is A -> Option<A>, not Option<A> -> Option<B>

@sfackler 's and_then is the function I was looking for.