Open In App

Rust – The dyn Keyword

Last Updated : 23 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Rust, we have dyn Keyword. We know that Struct is implemented via a trait by using the impl keyword and its own definitions are specified by using the trait methods.

Dyn keyword is generally used as a trait for the object’s type and is used for highlighting the methods by which the traits are dispatched dynamically. The main objective of using the dyn keyword is to ensure the safety of objects. Dyn trait has two pointers – one that links to the data and the other pointer calls a map of various method pointers (known as vtable). Dyn trait is called at runtime when a method needs to be called which in turn consults vtable for getting the function pointer. 

In this example, we declare a trait GFG and create two structs  => Practice and Courses. Then, we implement the GFG impl for Practice and Courses. 

Example 1:

Rust




// Rust program without use of dyn Keyword
#![allow(dead_code)]
  
fn main(){
    trait GFG { 
        fn pick(&self); 
    }
      
   //create a struct
    struct Practice; 
    struct Courses;  
    
    impl GFG for Practice {
        fn pick(&self) {
            println!("Lets practice questions from GFG");
        }
    }
    impl GFG for Courses {
        fn pick(&self) {
            println!("Lets buy courses from GFG");
        }
    }
  
    let gfg = Practice;
    g.pick();
}


Output:

 

To remove such difficulties, we use the dyn keyword.

The dyn keyword is used when we require to create trait objects. A trait object is defined as an object that contains objects of different data types at any instant of time. Traits are generally wrapped inside a box when during the creation of trait objects as the trait’s size is unknown at compile time.

Now take another example, Here we implement the GFG trait as well as two structs – Practice and courses. Next, we create a vector of the GFG trait and then call the pick() function in the list. 

Example 2:

Rust




// Rust program for The dyn keyword.
  
#![allow(dead_code)]
  
fn main(){
    trait GFG { 
        fn pick(&self);
    }
      
    // Create a struct 
    struct Practice; 
    struct Courses;  
  
    impl GFG for Practice {
        fn pick(&self) {
            println!("Lets practice questions from GFG");
        }
    }
    impl GFG for Courses {
        fn pick(&self) {
            println!("Lets buy courses from GFG");
        }
    }
  
   // Create a vector of GFG:
    let mut list: Vec<Box<dyn GFG>> = Vec::new();
    let practice = Practice;
    let course = Courses;
  
    list.push(Box::new(practice));
    list.push(Box::new(course));
  
    // Calling pick() for GFG options in the list:
    for gfg in &list{
        gfg.pick();
    }
}


Output:

 

Here, We implement GFG impl for struct Practice and impl GFG for struct Courses. In the pick() function, the self parameter is passed. Once the vector of GFG is created, we assign variables (practice and course) into their structs.

Next, we call the pick () function for pushing the GFG options in the list.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads