The Drop trait runs code when a value goes out of scope, executing in reverse order of creation (LIFO). Implement Drop by defining the drop method on your type to perform cleanup tasks like closing files or freeing resources.
use std::fmt::Debug;
struct MyResource;
impl Drop for MyResource {
fn drop(&mut self) {
println!("Cleaning up MyResource");
}
}
fn main() {
let _r = MyResource; // `drop` runs here when `_r` goes out of scope
}