-
-
Save g101418/a6f887e41dfd8ed19511ed171f9a3bad to your computer and use it in GitHub Desktop.
Rust有趣代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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