Tour · 12 of 25
for p in &mut players changes the elements in place, and the list holds still while you do
A plain for p in players binds each element as a read-only copy, so a loop that
writes p.score would change nothing, and the compiler rejects it. To change
the elements, loop over &mut players: each step binds a writable reference to
the element's slot. While such a reference lives, the list may not grow, shrink,
or move its elements, because the reference would point at moved memory. A
push in the loop's body panics with collection-pinned, and replacing or
sorting elements is still fine. &mut players[0] passed to a call holds the
list still for that call only, and no name may keep it: write
players[0].score += 1 instead. bonus takes &mut List[Player] so that it
changes the caller's list, not a copy.
Run: Ctrl+Enter · Pages: Alt+← Alt+→