Into/From for Result and Option not intuitive

I was recently surprised that the Into trait implementation for Result and Option is not to simply wrap the subject with Ok or Some respectively. It might be instructive if some could explain why this was done. There may be a need to effect elegant suppression of blanket implementations.

Your premise is false. impl<T> From<T> for Option<T> simply wraps in Some, and there is no equivalent impl for Result. There's also no explicit Into impl for either type, they are covered by the blanket impl.


Indeed. Thanks.

fn check() {
    let _ :Option<i32> = Some(23);
    let _ :Result<i32,()> = Ok(23);
    //the trait bound `Result<i32, ()>: From<{integer}>` is not satisfied
    // ...naively expected Ok(23)
    //let _: Result<i32,()> = 23.into();
    let x: Option<i32> = 23.into();

    //the trait bound `Result<i32, ()>: From<Option<i32>>` is not satisfied
    // ...naively expected Ok(x) for whatever x is.
    //let _: Result<i32,()> = x.into();
    let _: Result<i32,()> = x.ok_or(());

My journey into rust continues. :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.