Hacker News new | past | comments | ask | show | jobs | submit login

> // 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.




That is precisely correct. It also works before the variable has even been initialized to begin with: https://play.rust-lang.org/?gist=faf1642e4e1f48decbeac704505...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: