pretzelhammer / rust-blog

Educational blog posts for Rust beginners
Apache License 2.0
7.53k stars 396 forks source link

Request to add extra info: cannot implement both From and TryFrom #66

Closed bnm3k closed 1 month ago

bnm3k commented 1 year ago

In the TryFrom section, is it worth mentioning that you cannot implement both From and TryFrom since if From is implemented for a given type T, an infallible TryFrom is also auto-implemented for you. Therefore you end up with conflicting trait implementations for the same type. For example, let's say you implement both From and TryFrom for Point, you get the following error:

error[E0119]: conflicting implementations of trait `TryFrom<(i32, i32)>` for type `Point`
  --> src/main.rs:22:1
   |
22 | impl TryFrom<(i32, i32)> for Point {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T, U> TryFrom<U> for T
             where U: Into<T>;

For more information about this error, try `rustc --explain E0119`.
error: could not compile `foo` due to previous error
pretzelhammer commented 1 month ago

https://github.com/pretzelhammer/rust-blog/commit/bd47b4faa9d2e091f4076e26e3ee040155c8b8d3