How to fix this code? rust-lang/rust#51743

Here how to fix this code? Playground

fn main() {
    let mut option = MyOption::MySome(String::new());
    option.take();
}

enum MyOption<T> {
    MyNone,
    MySome(T),
}

impl<T> MyOption<T> {
    fn take(&mut self) -> Option<T> {
        match self {
            MyOption::MySome(value) => {
                *self = MyOption::MyNone;
                Some(value)
            },
            MyOption::MyNone => None,
        }
    }
}

You can look at how Option::take is implemented and do the same thing: playground.
But if MyOption is just what you're showing here I'd suggest you use the new type pattern.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.