Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.7 Simpler implementation of Drop? #309

Open
shanmahe opened this issue Dec 4, 2024 · 0 comments
Open

2.7 Simpler implementation of Drop? #309

shanmahe opened this issue Dec 4, 2024 · 0 comments

Comments

@shanmahe
Copy link

shanmahe commented Dec 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant