This is the partial borrow problem that has been discussed in a recent thread: Why can't I change a reference? - #16 by GRASBOCK The discussion contains a lot of info and suggestions which might be helpful.
In the (possibly contrived) example provided, this can be fixed in the following ways:
- Hoist the call to
s.add()
outside of the inner closure. E.g. calls.add()
before iteratings.val
- Split the
MyStruct
type so you do not need to take a shared reference while iterating over an exclusive reference. - Use an internally mutable type with
.par_iter()
(Effectively comes down to using a mutex, which you explicitly said you are not interested in...)
playground showing solutions 1 and 2.