1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-05 20:02:37 +02:00

Update implicit-conversions.md (#210)

I suggest to clarify that you only need to implement `From` and nobody really implements `Into` as soon as `From` is implemented.

I decided to add a separate paragraph after the `i8` and `i16` example, as they provide intuitive understanding of reverse relationship between `From` and `Into`. Alternatively trying to explain `From` and `Into` in generic way sounds more confusing than just adding this sentence after the specific example.
This commit is contained in:
Igor Petruk 2023-01-21 14:20:14 +00:00 committed by GitHub
parent 25a32abef7
commit 905d0a067e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,6 +27,9 @@ that we can convert a variable `x` of type `i8` to an `i16` by calling
`i16::from(x)`. Or, simpler, with `x.into()`, because `From<i8> for i16` `i16::from(x)`. Or, simpler, with `x.into()`, because `From<i8> for i16`
implementation automatically create an implementation of `Into<i16> for i8`. implementation automatically create an implementation of `Into<i16> for i8`.
The same applies for your own `From` implementations for your own types, so it is
sufficient to only implement `From` to get a respective `Into` implementation automatically.
1. Execute the above program and look at the compiler error. 1. Execute the above program and look at the compiler error.
2. Update the code above to use `into()` to do the conversion. 2. Update the code above to use `into()` to do the conversion.