When I first learnt about exclusive references "&mut T", they made sense because of "data races".
However, I didn't question when this would happen (hadn't learnt about parallel and concurrent code at that time), and simply remembered it.
But...what is the reason for &mut T when we running non-parallel or concurrent code, where there aren't data races?
In this case, I supposed there can not be any data races, at least not in the sense I conceptualise them, so the three clauses the book cites (excepting the second one):
- Two or more pointers access the same data at the same time.
- At least one of the pointers is being used to write to the data.
- There’s no mechanism being used to synchronize access to the data.
apply mostly to parallel or concurrent code, but not in all other cases.
One possible reason for &mut T is that it simply possibly makes code easier to follow.
Another reason is in cases where we may have 2 pointers and one of them may cause the data to be moved to a different place in memory (I don't think this is called data race, right?). For this case, AI gave a good example:
let mut v = vec![1, 2, 3];
let first = &v[0];
v.push(4);
println!("{}", first);
But this is still a subset of cases, as it wouldn't affect fixed sized things.
I was wondering of any other important reasons you would add?