Open In App

Rust – From and Into Traits

Last Updated : 09 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Rust, we have the From and Into Traits that are linked internally and are implemented in type, conversion scenarios say in scenarios where we convert from type A to type B.

The syntax for From Trait:

impl trait From<T> 

{
   fn from(T) -> Self;
}

From traits in Rust are mainly used for evaluating value conversions while taking input from users. From Traits is preferred over Into Traits due to the fact that From Traits because From Trait provides an implementation of Into function from its standard library. 

From Trait is extremely helpful in error handling i.e. when a function call fails, the return type of the function of the form Result<T, E>. The From trait in these situations is extremely helpful as it handles the function by allowing us to return a single error type which in turn encapsulates several error types.

The From Trait in rust helps us define a type from oneself to another. Its most common type is the conversion between primitive and other data types.

In this example, we define a _my_str variable and assign a value “GeeksforGeeks”. Before proceeding, we need to import the std::convert::From before converting the str into a String.

Example 1:

Rust




// Rust program for From Traits
 
use std::convert::From;
fn main() {
    let _my_str = "GeeksforGeeks";
      
    // converting an str into a String
    let _my_string = String::from(_my_str);
}


Output:

 

In the given below example, we define a struct that has a ‘year’ value defined by the i32 type. We use the impl function for the GFG struct that takes the GFG struct and the item value is passed onto it. After we define the main function, we let a variable num declare into the GFG struct (we pass 2022 value) using From trait.

As we see, the value of num gets passed here, and the output gets printed.

Example 2:

Rust




// Rust program for From Trait
use std::convert::From;
 
#[derive(Debug)]
struct GFG {
    year: i32,
}
 
impl From<i32> for GFG {
    fn from(item: i32) -> Self {
        GFG {year: item }
    }
}
 
fn main() {
    let num = GFG::from(2022);
    println!("learning Rust from {:?}", num);
}


Output:

 

Into Trait: 

The Into trait is just the opposite of From Trait. Once we have implemented the From Trait for our type, Into Trait is called when necessary. The into Trait requires certain specifications as we require certain specifications to convert as the compiler is not able to differentiate most of the time. Into Trait is mainly used in specifying trait bounds for generic functions that in turn ensure specific types are converted via the Into Trait.

Example 3:

Rust




// Rust program for Into Trait
use std::convert::From;
 
#[derive(Debug)]
struct GFG {
    year: i32,
}
 
impl From<i32> for GFG {
    fn from(item: i32) -> Self {
        GFG { year: item }
    }
}
 
fn main() {
    let int_var = 2022;
    let num: GFG = int_var.into();
    println!("Learning Rust from {:?}", num);
}


Output:

 

In this example, we first implement the std::convert::From function for the implementation of the Into Trait. We define a struct and use impl to pass it to the function. But this time, instead of assigning a new variable, we use the .into() method that is implemented from the std::convert::from.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads