Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

generics - How do I unwrap an arbitrary number of nested Option types?

I'm trying to write a trait that will allow me to "unwrap" multiple nested Option<Option<...<T>>>> to a single Option<T> to better work with an API I am using. I'm trying to create a generic solution, but I can't figure out how to make it work.

This is one of my many attempts:

trait UnwrapOption<T> {
    fn unwrap_opt(self) -> Option<T>;
}

impl<T> UnwrapOption<T> for Option<T> {
    fn unwrap_opt(self) -> Option<T> {
        self
    }
}

impl<T> UnwrapOption<T> for Option<Option<T>> {
    fn unwrap_opt(self) -> Option<T> {
        match self {
            Some(e) => e.unwrap_opt(),
            None => None,
        }
    }
}

fn main() {
    let x = Some(Some(Some(1)));
    println!("{:?}", x.unwrap_opt());
}
error[E0282]: type annotations needed
  --> src/main.rs:22:24
   |
22 |     println!("{:?}", x.unwrap_opt());
   |                      --^^^^^^^^^^--
   |                      | |
   |                      | cannot infer type for type parameter `T` declared on the trait `UnwrapOption`
   |                      this method call resolves to `Option<T>`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of flattening out the nested option, as the other answer shows, I'd advocate that you never create an Option<Option<T>> that you need to flatten in the first place. In the majority of cases I've seen, it's because someone misuses Option::map when they should have used Option::and_then:

fn main() {
    let input = user_input();

    let a = input.map(add1);
    // a is Option<Option<i32>>

    let b = input.and_then(add1);
    // a is Option<i32>
}

fn user_input() -> Option<i32> {
    Some(10)
}

fn add1(a: i32) -> Option<i32> {
    Some(a + 1)
}

Remember that Rust is a statically typed language; you will always know the exact level of nesting.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...