Skip to content
Related Articles

Related Articles

Rust – Drop Trait
  • Last Updated : 19 Apr, 2021

Drop trait lets us customize what happens when a value is about to go out of scope. Drop trait is important to the smart pointer pattern. 

Drop trait functionality is used when implementing a smart pointer. For example, Box<T> customizes Drop to deallocate the space on the heap that the box points to.

Drop trait can be used to clean up values when they go out of scope.

Syntax:

trait Drop {
fn drop(&mut self);
}

Variables are dropped in the reverse order of their creation, so d was dropped before c. In Rust, the drop is automatically called up when our instances go out of scope.



With the help of the given examples, it will be easy to understand that how the drop method works:

Example 1:

Rust




struct SmartPointer {
 data: String,
}
  
// implementing Drop trait
impl Drop for SmartPointer {
 fn drop(&mut self) {
 println!("Dropping SmartPointer with data `{}`!", self.data);
 }
}
fn main() {
 let _c = SmartPointer { data: String::from("my stuff") };
 let _d = SmartPointer { data: String::from("other stuff") };
 println!("SmartPointers created.");
 }


Output 1:

SmartPointers created.
Dropping SmartPointer with data `other stuff`!
Dropping SmartPointer with data `my stuff`!

Example 2:

Rust




struct New {
 data: String,
}
impl Drop for New {
 fn drop(&mut self) {
 println!("Dropping New with data `{}`!", self.data);
 }
}
fn main() {
 let c = New { data: String::from("some data") };
 println!("New created.");
 drop(c);
 println!("New dropped before the end of main.");
}


Output 2:

New created.
Dropping New with data `some data`!
New dropped before the end of main.

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

The std::mem::drop function is not the same as the drop method in the Drop trait. We can call it by passing the value we want to drop the value before the end of the scope. In Rust, we cannot call the Drop trait’s drop method manually but we have to call the std::mem::drop function. 

Example:

Rust




struct GeeksforGeeks  
{  
  a : String,  
}  
impl Drop for GeeksforGeeks  
{  
  fn drop(&mut self)  
  {  
    println!("Dropping the instance of GeeksforGeeks with data : {}", self.a);  
  }  
}  
fn main()  
{  
  let value1 = GeeksforGeeks{a : String::from("This is ")};  
  drop(value1);  
  let _value2 = GeeksforGeeks{a: String::from("GFG Platform")};  
  
}


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.

Output:

Dropping the instance of GeeksforGeeks with data : This is 
Dropping the instance of GeeksforGeeks with data : GFG Platform



My Personal Notes arrow_drop_up
Recommended Articles
Page :