rust-unofficial / too-many-lists

Learn Rust by writing Entirely Too Many linked lists
https://rust-unofficial.github.io/too-many-lists/
MIT License
3.18k stars 276 forks source link

Fifth: simplified by removing the deref in `pop` #207

Closed ChenZhongPu closed 2 years ago

ChenZhongPu commented 2 years ago

The pop function in fifth.rs can be simplified:

    pub fn pop(&mut self) -> Option<T> {
        self.head.take().map(|head| {
            let head = *head;
            self.head = head.next;

            if self.head.is_none() {
                self.tail = ptr::null_mut();
            }

            head.elem
        })
    }

Due to the auto-deref, let head = *head; can be removed.

Gankra commented 2 years ago

This section has been completely rewritten. Any of the old code is now a large "failed experiment" that leads into the "right" implementation, so I'm fine with it having some slop.