Menu
Coddy logo textTech

The Drop Trait

Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 56 of 61.

When a value goes out of scope in Rust, it's automatically cleaned up. But what if you need to run custom code at that moment—like logging, releasing a resource, or sending a final message? The Drop trait lets you define exactly what happens when your struct is about to be destroyed.

The Drop trait has a single method called drop that takes &mut self:

struct Connection {
    id: u32,
}

impl Drop for Connection {
    fn drop(&mut self) {
        println!("Connection {} closed.", self.id);
    }
}

You never call drop directly—Rust calls it automatically when the value goes out of scope:

fn main() {
    let conn = Connection { id: 42 };
    println!("Using connection...");
}  // "Connection 42 closed." prints here automatically

The output would be:

Using connection...
Connection 42 closed.

This is Rust's version of a destructor. It's commonly used for cleanup tasks: closing file handles, disconnecting from servers, or freeing resources that Rust can't manage automatically. The Drop trait guarantees your cleanup code runs, even if the scope ends due to an early return.

challenge icon

Challenge

Easy

Let's build a resource management system that demonstrates automatic cleanup! You'll create a FileHandle struct that simulates opening and closing files—when the handle goes out of scope, it will automatically announce that the file has been closed.

You'll organize your code across two files:

  • file_handle.rs: Define a public FileHandle struct with a public filename field (String). Implement a new associated function that creates a FileHandle and prints a message when the file is "opened". Then implement the Drop trait so that when the handle is dropped, it prints a closing message.
  • main.rs: Bring in your file_handle module and create a function called process_file that takes a filename, creates a FileHandle inside a local scope, and prints a processing message while the file is "open". The Drop implementation will automatically run when the scope ends. Call this function with the provided input.

When a FileHandle is created, it should print:

Opening file: {filename}

While processing (inside the scope), print:

Processing {filename}...

When the FileHandle is dropped (scope ends), it should print:

Closing file: {filename}

For example, with input data.txt:

Opening file: data.txt
Processing data.txt...
Closing file: data.txt

And with input config.json:

Opening file: config.json
Processing config.json...
Closing file: config.json

You will receive one input: the filename string.

Cheat sheet

The Drop trait allows you to define custom cleanup code that runs automatically when a value goes out of scope.

The Drop trait has a single method called drop that takes &mut self:

struct Connection {
    id: u32,
}

impl Drop for Connection {
    fn drop(&mut self) {
        println!("Connection {} closed.", self.id);
    }
}

You never call drop directly—Rust calls it automatically when the value goes out of scope:

fn main() {
    let conn = Connection { id: 42 };
    println!("Using connection...");
}  // "Connection 42 closed." prints here automatically

The Drop trait is commonly used for cleanup tasks like closing file handles, disconnecting from servers, or freeing resources. It guarantees your cleanup code runs, even if the scope ends due to an early return.

Try it yourself

mod file_handle;

use file_handle::FileHandle;

// TODO: Implement the process_file function
// It should:
// 1. Create a local scope (using curly braces {})
// 2. Inside that scope, create a FileHandle using FileHandle::new()
// 3. Print the processing message: "Processing {filename}..."
// 4. When the scope ends, Drop will automatically be called
fn process_file(filename: &str) {
    // TODO: Implement this function
}

fn main() {
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let filename = input.trim();
    
    process_file(filename);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming