From and Into

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.

From

The From trait allows for a type to define how to create itself from another
type, hence providing a very simple mechanism for converting between several
types. There are numerous implementations of this trait within the standard
library for conversion of primitive and common types.

For example we can easily convert a str into a String

  1. let my_str = "hello";
  2. let my_string = String::from(my_str);

We can do similar for defining a conversion for our own type.

  1. use std::convert::From;
  2. #[derive(Debug)]
  3. struct Number {
  4. value: i32,
  5. }
  6. impl From<i32> for Number {
  7. fn from(item: i32) -> Self {
  8. Number { value: item }
  9. }
  10. }
  11. fn main() {
  12. let num = Number::from(30);
  13. println!("My number is {:?}", num);
  14. }

Into

The Into trait is simply the reciprocal of the From trait. That is, if you
have implemented the From trait for your type you get the Into
implementation for free.

Using the Into trait will typically require specification of the type to
convert into as the compiler is unable to determine this most of the time.
However this is a small trade-off considering we get the functionality for free.

  1. use std::convert::From;
  2. #[derive(Debug)]
  3. struct Number {
  4. value: i32,
  5. }
  6. impl From<i32> for Number {
  7. fn from(item: i32) -> Self {
  8. Number { value: item }
  9. }
  10. }
  11. fn main() {
  12. let int = 5;
  13. // Try removing the type declaration
  14. let num: Number = int.into();
  15. println!("My number is {:?}", num);
  16. }