Alright, so I was surprised to learn that in Rust, you can build more methods into primitive data types. We have our struct, a number, just a simple structure, and we can implement a trait onto another data type that's already defined by Rust or someone else. How neat is that?
The 'into trait' is the opposite or, another way to think of it, the reciprocal of the 'from trait'. We can have two kinds of data structures, our number struct and an i32. Whichever one you decide to create first, you can convert that type into the other one.
That's how Rust does it. We're implementing the 'into trait' for i32 which will convert the i32 into a number (which is a struct). We're returning a number and it's pretty straightforward.
The syntax is pretty cool if you ask me. The compiler will know what to do if you put any sort of i32 value (which is the default number value), and then 'into' a number struct. It'll interpret that and instantiate your struct with the value in the right place.
But it also needs to know what you want to convert it into, even though you're calling upon the trait. It doesn't know which trait to call upon that's been defined. It's got to be a number, there's no other options.
Rust comes with default type conversions for some of your data types but not others, which I thought was interesting. One thing I learned was that Rust doesn't let you do redundant conversions. If you try to implement both 'from' and 'into' at the same time for the same data types, it just won't let you.
In the end, what I wanted to do was implement a trait that lets me convert a number into an i-32, the opposite direction. I was able to define a number with the structure, capturing data from an i-32, then I was able to convert it the other direction through the into function. This gives us the chance to do type conversion with complex structures, better still, it's a built-in feature of Rust.
What a nifty pattern!