Std lib should implement From<Option<T>> for Option<E> where T: Into<E>

Rust's Option type should allow for the following:

let foo: Option<String> = Some("hello").into();

This could be done as &str implements From<String>.

The implementation would have the following signature:

impl<T, E> From<Option<T>> for Option<E>
  T: Into<E>

And the conversion function would simply be:

fn from(val: Option<T>) -> Self {

On a side note, it would also be nice to be able to go from &str into Option<String> with an implementation of impl From<&str> for Option<String>.

Since T: Into<T> this would generate an impl of From<Option<T> for Option<T>, which would conflict with the blanket impl of From<T> for T.


Once specialization lands, though, I wouldn't mind seeing this.

1 Like

I doubt this would be possible with the current specialization rules because neither impl specializes the other, they're only overlapping. This case is mentioned in the specialization RFC under the "lattice rule", which is just an idea for future improvement and not something formalized and/or accepted.


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