Skip to content

Instantly share code, notes, and snippets.

@g101418
Created September 1, 2022 12:45
Show Gist options
  • Save g101418/a6f887e41dfd8ed19511ed171f9a3bad to your computer and use it in GitHub Desktop.
Save g101418/a6f887e41dfd8ed19511ed171f9a3bad to your computer and use it in GitHub Desktop.
Rust有趣代码
impl Drop for List {
fn drop(&mut self) {
let mut cur_link = mem::replace(&mut self.head, Link::Empty);
while let Link::More(mut boxed_node) = cur_link {
cur_link = mem::replace(&mut boxed_node.next, Link::Empty);
// boxed_node 在这里超出作用域并被 drop,
// 由于它的 `next` 字段拥有的 `Node` 被设置为 Link::Empty,
// 因此这里并不会有无边界的递归发生
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment