Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From/Into section 6.1 introduction confusing #1149

Closed
mitchblevins opened this issue Jan 26, 2019 · 3 comments
Closed

From/Into section 6.1 introduction confusing #1149

mitchblevins opened this issue Jan 26, 2019 · 3 comments

Comments

@mitchblevins
Copy link

The introductory text of section 6.1 suggests that the reciprocal Into trait for a From trait does the conversion in the opposite direction. (type A to B, type B to A).

This is confusing because the conversion is always type A to B, but the From/Into difference is which type it is initiated by.

@jonmccune
Copy link

I'm a random Rust n00b going through rust-by-example, and I also noticed this. After staring at it for a while, I actually think it would be an improvement if the first two sentences were simply deleted. I.e., just drop:

"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."

@jakehemmerle
Copy link

jakehemmerle commented Mar 9, 2023

Hi, I thought this was confusing too and I came to open an issue.

The example in the book only shows how to convert A to B (using both B::from(a) and a.into()`), but it very well suggests we can convert A to B as well as B to A.

These two points should be emphasized:

  1. The From implementation is preferred to Into
  2. Into is automatically implemented on the destination type (A in our case),

There was a third claim on the above, closed, PR:

3.Into must be implemented instead of From when both types are not defined by the current crate.

3 appears to be not true, according to this error I got today:

error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
  --> crypto/shared/src/constraints.rs:27:1
   |
27 | impl Into<sp_core::H160> for H160 {
   | ^^^^^-------------------^^^^^----
   | |    |                       |
   | |    |                       `primitive_types::H160` is not defined in the current crate
   | |    `sp_core::H160` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

If someone elaborate on the idiomatic way to allow for type conversion between types in different crates, I will open a new PR with these changes. Using a new arbitrary trait seems dirty. Thanks!

@jakehemmerle
Copy link

cc @marioidival for visibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants