Puzzle 7: Explanation
Explore how Rust automatically implements the Into trait when the From trait is defined, enabling smooth type conversions. Learn to implement custom conversions using From and TryFrom traits, manage possible failures, and leverage strong types to minimize bugs by enforcing unit and range constraints in your Rust code.
We'll cover the following...
Test it out
Hit “Run” to see the code’s output.
Explanation
The surprise here is that the Into trait wasn’t implemented, yet the program could still use the into() function with the Radians type.
When we define the From trait, Rust automatically implements the reciprocal Into trait for us. This is very convenient and surprising, given Rust’s general insistence on the behavior being defined explicitly.
Prior to Rust version 1.4.1, this automatic implementation was only performed for types accessible from the crate ...