How can I "add" a flatten()
method to Option<U>
, which would only typecheck if and only if U
is an Option<T>
, or more simply to add it to Option<Option<T>>
? Naively, I am trying to write the following, which doesn't compile:
impl Option<Option<T>> {
fn flatten(&self) -> Option<T> {
match self {
None => None,
Some(v) => v,
}
}
}
fn main() {
let x = Some(Some(1));
let y = x.flatten();
println!("{:?}", y);
}