profile
viewpoint

Ask questionssecond "let new_tail =" shadows first one in push!

In the second version of push in https://rust-unofficial.github.io/too-many-lists/fifth-layout.html it is confusing to have two definitions of new_tail, the second shadowing the first

pub fn push(&mut self, elem: T) {
    // FIRST DEFINITION:
    let new_tail = Box::new(Node {
        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
    // SECOND DEFINITION SHADOWING FIRST ONE
    let new_tail = match self.tail.take() {
        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_mut().map(|node| &mut **node)
        }
        None => {
            // Otherwise, update the head to point to it
            self.head = Some(new_tail);
            self.head.as_mut().map(|node| &mut **node)
        }
    };

    self.tail = new_tail;
}

Is this a hint that they will wind up having the same value?

rust-unofficial/too-many-lists

Answer questions Gankra

This is kinda a standard rust idiom for expressing the same logical value changing type as you refine/compute it. But also it's kinda just confusing here?

useful!

Related questions

First example fails to induce E0072 hot 1
source:https://uonfu.com/
Github User Rank List