rust-lang/rust-by-example

Slightly misleading wording in "From and Into" section (6.1)

morzel85 opened this issue · 2 comments

The https://doc.rust-lang.org/rust-by-example/conversion/from_into.html page states:

The From and Into traits are inherently linked, and this is actually part of its implementation. If you are able to convert type A from type B, then it should be easy to believe that we should be able to convert type B to type A.

This is correct but a bit awkward to read and might suggests that if I have a type Potato, which is convertible to MashedPotato, then it should be possible to convert MashedPotato back to Potato.

use std::convert::From;

#[derive(Debug)]
struct Number {
    value: i32,
}

impl From<i32> for Number {
    fn from(item: i32) -> Self {
        Number { value: item }
    }
}

fn main() {
    let num1 = Number::from(1);
    let num2: Number = 2.into();
    println!("My number with from is {:?}", num1);
    println!("My number with into is {:?}", num2);

    // This won't work unless conversion is implemented:
    // let int = i32::from(num1);
}

I have a suggestion:

The From and Into features are inherently linked and this is actually part of their implementation. It should be easy to believe that we would be able to convert type B to type A, since we are able to convert type A to type B if From and Into are implemented.

What you think?

Hi, I'm closing this issue just to clean up the items that already exist. If you think this issue makes sense to stay open, please create a new issue with updated information and mention this one.

thanks!