Rust 学习笔记:Drop trait
Rust 学习笔记:Drop trait
智能指针模式的第二个重要特性是 Drop,它允许自定义当值即将超出作用域时发生的事情。可以在任何类型上提供 Drop trait 的实现。
在实现智能指针时几乎总是使用 Drop trait。例如,当删除 Box<T> 时,它将释放该指针所指向的堆上的空间。
Drop trait 要求实现一个 drop 方法,该方法接受对 self 的可变引用。
为了了解 Rust 何时调用 drop,编写一个示例如下:
struct CustomSmartPointer {
data: String,
}
impl Drop for CustomSmartPointer {
fn drop(&mut self) {
println!("Dropping CustomSmartPointer with data `{}`!", self.data);
}
}
fn main() {
let c = CustomSmartPointer {
data: String::from("my stuff"),
};
let d = CustomSmartPointer {
data: String::from("other stuff"),
};
println!("CustomSmartPointers created.");
}
我们在 CustomSmartPointer 上实现了 Drop trait,drop 方法将在 CustomSmartPointer 类型的实例超出作用域时自动被调用。
Drop trait 在 prelude 中,无需手动引入。
程序输出:
我们发现,变量的删除顺序与创建的顺序相反。
Drop trait 的全部意义在于它是自动处理的。
Rust 不允许手动调用 Drop trait 的 drop 方法。
修改之前的代码:
fn main() {
let c = CustomSmartPointer {
data: String::from("some data"),
};
println!("CustomSmartPointer created.");
c.drop();
println!("CustomSmartPointer dropped before the end of main.");
}
报错:
此错误消息指出不允许显式地调用 drop 方法,因为 Rust 在 main 函数末尾自动调用 drop 方法。
错误消息使用术语析构函数,这是用于清理实例的函数的通用编程术语。Rust 中的 drop 函数是一个特殊的析构函数。
如果希望在值的作用域结束之前强制删除值,则必须调用标准库提供的 std::mem::drop 函数。
std::mem::drop 函数不会干扰 Rust 的自动清理机制。因为该函数会接管值的所有权并销毁,不会导致 double free error。
修改之前的代码:
fn main() {
let c = CustomSmartPointer {
data: String::from("some data"),
};
println!("CustomSmartPointer created.");
drop(c);
println!("CustomSmartPointer dropped before the end of main.");
}
程序输出:
从输出可以看出,CustomSmartPointer 示例在 main 函数结束前被删除。
有了 Drop trait 和 Rust 的所有权系统,可以实现资源的自动释放。确保引用始终有效的所有权系统还确保仅在值不再使用时调用 drop 一次。