Drop
Трейт Drop має лише один метод: drop, який викликається автоматично,
коли об’єкт виходить за межі області видимості. Основне використання трейта Drop — звільнити
ресурси, якими володіє екземпляр, що реалізує його.
Box, Vec, String, File і Process — це кілька прикладів типів, які
реалізують трейт Drop для звільнення ресурсів. Трейт Drop також може бути
реалізований вручну для будь-якого власного типу даних.
У наступному прикладі до функції drop додається виведення в консоль, щоб повідомити
коли її викликано.
struct Droppable {
name: &'static str,
}
// This trivial implementation of `drop` adds a print to console.
impl Drop for Droppable {
fn drop(&mut self) {
println!("> Dropping {}", self.name);
}
}
fn main() {
let _a = Droppable { name: "a" };
// block A
{
let _b = Droppable { name: "b" };
// block B
{
let _c = Droppable { name: "c" };
let _d = Droppable { name: "d" };
println!("Exiting block B");
}
println!("Just exited block B");
println!("Exiting block A");
}
println!("Just exited block A");
// Variable can be manually dropped using the `drop` function
drop(_a);
// TODO ^ Try commenting this line
println!("end of the main function");
// `_a` *won't* be `drop`ed again here, because it already has been
// (manually) `drop`ed
}
Для більш практичного прикладу ось як трейт Drop можна використати, щоб автоматично
очищати тимчасові файли, коли вони більше не потрібні:
use std::fs::File;
use std::path::PathBuf;
struct TempFile {
file: File,
path: PathBuf,
}
impl TempFile {
fn new(path: PathBuf) -> std::io::Result<Self> {
// Note: File::create() will overwrite existing files
let file = File::create(&path)?;
Ok(Self { file, path })
}
}
// When TempFile is dropped:
// 1. First, our custom drop implementation runs. The file is still open at this point,
// but we can remove it from the filesystem by path.
// 2. Then, after our drop returns, Rust automatically drops each field,
// so File's drop runs and closes the file handle.
impl Drop for TempFile {
fn drop(&mut self) {
// Note: the File is still open here — field destructors run after this method.
if let Err(e) = std::fs::remove_file(&self.path) {
eprintln!("Failed to remove temporary file: {}", e);
}
println!("> Dropped temporary file: {:?}", self.path);
// After this method returns, Rust will drop each field (including `file`),
// which closes the underlying file handle.
}
}
fn main() -> std::io::Result<()> {
// Create a new scope to demonstrate drop behavior
{
let temp = TempFile::new("test.txt".into())?;
println!("Temporary file created");
// File will be automatically cleaned up when temp goes out of scope
}
println!("End of scope - file should be cleaned up");
// We can also manually drop if needed
let temp2 = TempFile::new("another_test.txt".into())?;
drop(temp2); // Explicitly drop the file
println!("Manually dropped file");
Ok(())
}