Assuming I have an enum as:
enum Foo {
A,
B,
}
...where I want to implement something like this:
impl From<char> for Option<Foo> {
fn from(chr: char) -> Self {
match chr {
'A' => Some(Foo::A),
'B' => Some(Foo::B),
_ => None,
}
}
}
This is currently illegal since Option is out of my own crate, it is owned by std. The compiler error is:
only traits defined in the current crate can be implemented for types defined outside of the crate
define and implement a trait or new type instead rustc(E0117)
As in the case I have shown above, in some cases, I'd like to have None instead of Some. Is there a way to implement a From that I am able to get Option<U> instead of U itself?
Environment
- Rust 1.62.1