Using self or parent inside the closure

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:

  1. Hoist the call to s.add() outside of the inner closure. E.g. call s.add() before iterating s.val
  2. Split the MyStruct type so you do not need to take a shared reference while iterating over an exclusive reference.
  3. 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.