In the process of writing a borrow-checker I've often turned to see what rust does, and I've often been surprised and confused. My mental model of the borrow-checker turns out to be missing a lot of detail .
I've also shown these confusing examples to other people and they've given me all sorts of confidently wrong explanations, so I suspect most people who write rust regularly are missing these details too.
Once you see the answer it's easy to believe that you already knew, so try to explain each example before clicking reveal!
The left-hand place expression evaluates to a mutable reference to x , but then the right-hand value expression reads from x . This shouldn't be allowed! Why does it work?
The right-hand expression is evaluated first , so the right-hand value expression reads from x before the left-hand place expression produces a mutable reference to x .
However, you may know that += is just sugar for add_assign . If we desugar the above example, does it look a little less obvious?