diff --git a/src/too-many-lists/advanced-lists/double-singly.md b/src/too-many-lists/advanced-lists/double-singly.md index 02607186..bb9e1674 100644 --- a/src/too-many-lists/advanced-lists/double-singly.md +++ b/src/too-many-lists/advanced-lists/double-singly.md @@ -71,9 +71,8 @@ impl Stack { impl Drop for Stack { fn drop(&mut self) { - let mut cur_link = self.head.take(); - while let Some(mut boxed_node) = cur_link { - cur_link = boxed_node.next.take(); + while let Some(box_node) = self.head.take() { + self.head = box_node.next; } } } diff --git a/src/too-many-lists/bad-stack/final-code.md b/src/too-many-lists/bad-stack/final-code.md index a71e85c0..6672e8ab 100644 --- a/src/too-many-lists/bad-stack/final-code.md +++ b/src/too-many-lists/bad-stack/final-code.md @@ -137,9 +137,8 @@ impl Drop for Node { ```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); + while let Link::More(box_node) = mem::replace(&mut self.head, Link::Empty) { + self.head = box_node.next; // boxed_node 在这里超出作用域并被 drop, // 由于它的 `next` 字段拥有的 `Node` 被设置为 Link::Empty, // 因此这里并不会有无边界的递归发生 @@ -222,10 +221,8 @@ impl List { 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); + while let Link::More(box_node) = mem::replace(&mut self.head, Link::Empty) { + self.head = box_node.next; } } } diff --git a/src/too-many-lists/ok-stack/itermut.md b/src/too-many-lists/ok-stack/itermut.md index 7bcf3910..5e195435 100644 --- a/src/too-many-lists/ok-stack/itermut.md +++ b/src/too-many-lists/ok-stack/itermut.md @@ -193,9 +193,8 @@ impl List { impl Drop for List { fn drop(&mut self) { - let mut cur_link = self.head.take(); - while let Some(mut boxed_node) = cur_link { - cur_link = boxed_node.next.take(); + while let Some(box_node) = self.head.take() { + self.head = box_node.next; } } } diff --git a/src/too-many-lists/ok-stack/type-optimizing.md b/src/too-many-lists/ok-stack/type-optimizing.md index 778f78ae..10624912 100644 --- a/src/too-many-lists/ok-stack/type-optimizing.md +++ b/src/too-many-lists/ok-stack/type-optimizing.md @@ -53,9 +53,8 @@ impl List { impl Drop for List { fn drop(&mut self) { - let mut cur_link = mem::replace(&mut self.head, None); - while let Some(mut boxed_node) = cur_link { - cur_link = mem::replace(&mut boxed_node.next, None); + while let Some(box_node) = mem::replace(&mut self.head, None) { + self.head = box_node.next; } } } @@ -103,9 +102,8 @@ impl List { impl Drop for List { fn drop(&mut self) { - let mut cur_link = self.head.take(); - while let Some(mut boxed_node) = cur_link { - cur_link = boxed_node.next.take(); + while let Some(box_node) = self.head.take() { + self.head = box_node.next; } } } @@ -177,9 +175,8 @@ impl List { impl Drop for List { fn drop(&mut self) { - let mut cur_link = self.head.take(); - while let Some(mut boxed_node) = cur_link { - cur_link = boxed_node.next.take(); + while let Some(box_node) = self.head.take() { + self.head = box_node.next; } } } diff --git a/src/too-many-lists/persistent-stack/drop-arc.md b/src/too-many-lists/persistent-stack/drop-arc.md index 6b7ca2a9..61c6cd44 100644 --- a/src/too-many-lists/persistent-stack/drop-arc.md +++ b/src/too-many-lists/persistent-stack/drop-arc.md @@ -5,9 +5,8 @@ ```rust impl Drop for List { fn drop(&mut self) { - let mut cur_link = self.head.take(); - while let Some(mut boxed_node) = cur_link { - cur_link = boxed_node.next.take(); + while let Some(box_node) = self.head.take() { + self.head = box_node.next; } } }