Skip to content

2.7 Simpler implementation of Drop? #309

@shantam-maheshwari

Description

@shantam-maheshwari

I wasn't familiar with the concept of tail recursion so didn't understand Section 2.7 fully, but this is what I came up with for implementing the Drop trait:

impl Drop for List {
    fn drop(&mut self) {
        while let Link::More(node) = mem::replace(&mut self.head, Link::Empty) {
            self.head = node.next;
        }
    }
}

Q: Is it logically equivalent to the code in the book? If so, this is more elegant imo.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions