Skip to content

The intermediate implementation in 6.1 is totally off #320

Description

@wf227

This code is unfortunately completely misleading:

pub fn push(&mut self, elem: T) <here is no return code mentioned> {
        let new_tail = Box::new(Node {                   new_tail is holding the Box
            elem: elem,
            // When you push onto the tail, your next is always None
            next: None,
        });

        // Put the box in the right place, and then grab a reference to its Node
        let new_tail = match self.tail.take() {         new_tail is now something complete different and the Box is dropped
            Some(old_tail) => {
                // If the old tail existed, update it to point to the new tail
                old_tail.next = Some(new_tail);
                old_tail.next.as_deref_mut() <- but this is returning a mut reference
            }
            None => {
                // Otherwise, update the head to point to it
                self.head = Some(new_tail);
                self.head.as_deref_mut()     < - and this returns also a mut reference
            }

Is push() in this intermediate version meant to return something and is new_tail holding the box until it is linked and has a new owner?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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