It would be nice if Rust's Option
provided some additional convenience methods like Option#flatten
and Option#flat_map
, where flatten
would reduce an <Option<Option<T>>
to Option<T>
, and flat_map
would work like map
, but takes a method/closure that returns an Option
and flattens it.
flat_map
is pretty straightforward:
fn opt_flat_map< T, U, F: FnOnce(T) -> Option<U> >(opt: Option<T>, f: F) -> Option<U> {
match opt {
Some(x) => f(x),
None => None
}
}
flatten
is more complex, and I don't really know how to go about defining it. It might look something like:
fn opt_flatten<T, U>(opt: Option<T>) -> Option<U> {
match opt {
Some( Some(x) ) => flatten_option( Some(x) ),
_ => opt
}
}
But that certainly doesn't work. Any thoughts?
Also, how would I go about implementing these methods on the Option
enum, so that I can use them natively on an Option
instance? I know I need to add the type signature in somewhere around impl OptionExts for Option<T>
, but I'm at a loss...
Hope this makes sense and I apologize for my imprecise terminology--I'm brand new to Rust.
In my case, I was dealing with an
Option
-returning method inunwrap_or_else
and forgot about plainor_else
method.These probably already exist, just as different names to what you expect. Check the docs for Option.
You'll see
flat_map
more normally asand_then
:The bigger way of doing what you want is to declare a trait with the methods you want, then implement it for
Option
:For
flatten
, I'd probably write:But if you wanted a trait:
See How do I unwrap an arbitrary number of nested Option types?