Industrial Training




Rust Drop Trait

    Drop trait is used to release the resources like files or network connections when the value goes out of the scope.
    Drop trait is used to deallocate the space on the heap that the Box< T> points to. The drop trait is used to implement the drop() method that takes a mutable reference to the self.

Let's see a simple example:
 struct Example  
{  
  a : i32,  
 }  
      impl Drop for Example  
{  
  fn drop(&mut self)  
  {  
    println!("Dropping the instance of Example with data : {}", self.a);  
  }  
}  
      fn main()  
{  
  let a1 = Example{a : 10};  
  let b1 = Example{a: 20};  
  println!("Instances of Example type are created");  
}  

Output:
Instances of Example type are created
Dropping the instance of Example with data : 20
Dropping the instance of Example with data : 10

Program Explanation


    We have implemented the Drop trait on the type Example, and we define the drop() method inside the implementation of the Drop trait.
    Inside the main() function, we create the instances of the type Example and at the end of the main() function, instances go out of the scope.
    When the instances move out of the scope, then Rust calls the drop() method implicitly to drop the instances of type Example. First, it will drop the b1 instance and then a1 instance.

Note: We do not need to call the drop() method explicitly. Therefore, we can say that the Rust calls the drop() method implicitly when our instances go out of the scope.


Dropping a value early with std::mem::drop


Sometimes, it becomes necessary to drop the value before the end of the scope. If we want to drop the value early, then we use the std::mem::drop function to drop the value.
Let's see a simple example to drop the value manually:


 struct Example  
{  
  a : String,  
}  
impl Drop for Example  
{  
  fn drop(&mut self)  
  {  
    println!("Dropping the instance of Example with data : {}", self.a);  
  }  
}  
fn main()  
{  
  let a1 = Example{a : String::from("Hello")};  
  a1.drop();  
  let b1 = Example{a: String::from("World")};  
  println!("Instances of Example type are created");  
}  

Output:

In the above example, we call the drop() method manually. The Rust compiler throws an error that we are not allowed to call the drop() method explicitly. Instead of calling the drop() method explicitly, we call the std::mem::drop function to drop the value before it goes out of the scope.


    The syntax of std::mem::drop function is different from the drop() function defined in the Drop trait. The std::mem::drop function contains the value passed as an argument which is to be dropped before it goes out of the scope.

Let's see a simple example:
 struct Example  
{  
  a : String,  
}  
  
impl Drop for Example  
{  
  fn drop(&mut self)  
  {  
    println!("Dropping the instance of Example with data : {}", self.a);  
  }  
}  
  
fn main()  
{  
  let a1 = Example{a : String::from("Hello")};  
  drop(a1);  
  let b1 = Example{a: String::from("World")};  
  println!("Instances of Example type are created");  
}  

Output:
Dropping the instance of Example with data : Hello
Instances of Example type are created
Dropping the instance of Example with data : World

In the above example, the a1 instance is destroyed by passing the a1 instance as an argument in the drop(a1) function.




Hi I am Pluto.