3 ms·
> // I actually don't understand why the line below compiles. > // Since `head` was moved into the box, I'm not sure why I can mutate it. > head.next = Some(B
by hcs 9y ago
> // I actually don't understand why the line below compiles.
> // Since `head` was moved into the box, I'm not sure why I can mutate it.
> head.next = Some(Box::new(next));
I'm fairly new to Rust myself, but it's my understanding that since it was moved into the Box, the variable "head" is now just considered effectively uninitialized, so you can go ahead and set its fields, or overwrite it entirely with head = Node {...}, without affecting the value that was moved into the Box.
- Rusky 9y agoThat is precisely correct. It also works before the variable has even been initialized to begin with: https://play.rust-lang.org/?gist=faf1642e4e1f48decbeac7045051113b&version=stable https://play.rust-lang.org/?gist=faf1642e4e1f48decbeac704505...