Currently working on a problem where I reverse a linked list. I was able to successfully write it but I feel like I may have an invalid number of mutable and immutable references. Rubberducking here by explaining what I think is happening but let me know in more detail!
I have marked the important lines above.
// 1: I am moving the wrapped box of "next" into the variable "current" in the scoped within the loop. This is not a copy if my understanding is correct but a move.
//2: I am moving current.next to next (which is currently invalid?) thus invalidating current.next
//3: I am moving prev to current.next (which is currently invalid?) thus invalidating prev
//4: ??? Why can I now move the value from current- I thought i just invalidated one of its fields by moving
Staying within your terminology, current.next became valid again when you moved the value from prev into it in the previous line // 3.
Instead of valid one could also call it initialized and instead of invalid there's some choice of wording amongst uninitialized/deinitialized/moved-out-of. (I don't know what the best terms would be per say.)
The compiler is taking track of all fields of values such as current. When current.next becomes moved-out-of, the whole value current is considered partially moved-out-of. The effect is that both variables can no longer be used except for assigning a new value (or accessing fields that are still valid). By re-initializing the .next field with a new value, all fields of current are in an initialized state again (the other field current.val was never invalidated) and the whole value becomes usable again.