Maxwell DeMers

@maxuuell

Leverage the Drop Trait as a Hook in Rust

Chapter 4 of the Rust Book outlines the three rules of ownership:

What struck me while working on the Rust WASM Intro was the profound impact the last rule has.

At the end of a resource's lifetime, which typically aligns with the scope the resource is valid in, the resource will be freed.

That behavior is driven by the Drop trait, which has only one method, drop.

The Drop trait is implemented by default on many types, but can be implemented on any custom type as well.

That Drop trait allows us to hook into the final milliseconds of the type's lifetime, and add custom logic.

We can essentially hook into the deallocation phase of the type, and do any last task, like logging to capture state, or IO operations.

Let's see a trivial example:

struct WickedWitch {
origin : &'static str
}
fn main() {
let witch = WickedWitch { origin : "west" };
}
impl Drop for WickedWitch {
fn drop(&mut self) {
println!("{}", "What a world!!")
}
}

When our program ends, when main finishes, you will see a print of "What a world!!"

You can check out the playground here.

Something more useful might be to capture the state of you struct:

use std::fs::File;
use std::io::prelude::*;
struct WickedWitch {
origin: &'static str,
}
fn main() {
let witch = WickedWitch { origin: "west" };
}
impl Drop for WickedWitch {
fn drop(&mut self) {
let mut file = File::create("origins.txt").unwrap();
let string_to_write = format!("WickedWitch origin: {}", &self.origin);
file.write(string_to_write.as_bytes());
}
}

This will create a file called origins.txt, and write the origin of the WickedWitch for later consumption.

Happy coding 🆓



Join My Mailing List

Subscribe to get my latest content by email.

    Unsubscribe at any time.