Drop
pub trait Drop {
fn drop(&mut self);
}list -> A -> B -> Cimpl Drop for List {
fn drop(&mut self) {
// 注意:在实际Rust代码中你不能显式调用`drop`,
// 我们假装自己是编译器!
list.head.drop(); // 尾递归——好!
}
}
impl Drop for Link {
fn drop(&mut self) {
match list.head {
Link::Empty => {} // 完成!
Link::More(ref mut boxed_node) => {
boxed_node.drop(); // 尾递归——好!
}
}
}
}
impl Drop for Box<Node> {
fn drop(&mut self) {
self.ptr.drop(); // 糟糕,不是尾递归!
deallocate(self.ptr);
}
}
impl Drop for Node {
fn drop(&mut self) {
self.next.drop();
}
}提前优化
Last updated