From: Ralf Jung Date: Sun, 19 Jul 2015 11:02:09 +0000 (+0200) Subject: Tune part 16 X-Git-Url: https://git.ralfj.de/rust-101.git/commitdiff_plain/174314786770835cff60100f9ea66aeec59745a4?hp=-c Tune part 16 --- 174314786770835cff60100f9ea66aeec59745a4 diff --git a/src/part16.rs b/src/part16.rs index fefccdf..5ae6a48 100644 --- a/src/part16.rs +++ b/src/part16.rs @@ -166,7 +166,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { //@ The linked list we wrote is already working quite nicely, but there is one problem: When the list is dropped, //@ nobody bothers to deallocate the remaining nodes. Even worse, if `T` itself has a destructor that needs to //@ clean up, it is not called for the element remaining in the list. We need to take care of that ourselves. -//@ + //@ In Rust, adding a destructor for a type is done by implementing the `Drop` trait. This is a very special trait. //@ It can only be implemented for *nominal types*, i.e., you cannot implement `Drop` for `&mut T`. You also cannot //@ restrict the type and lifetime parameters further than the type does - the `Drop` implementation has to apply to *all* instances diff --git a/workspace/src/part16.rs b/workspace/src/part16.rs index d0b173a..835b8b8 100644 --- a/workspace/src/part16.rs +++ b/workspace/src/part16.rs @@ -94,6 +94,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { // Add testcases for both kinds of iterators. // ## `Drop` + impl Drop for LinkedList { // The destructor itself is a method which takes `self` in mutably borrowed form. It cannot own `self`, because then // the destructor of `self` would be called at the end pf the function, resulting in endless recursion...