Is there a general way to access a container with mutable elements?

I would like to mutate items in a container. The only thing I care about is the length of the container and the fact that the elements of the container are ordered (i.e. there is a first element, a second element, etc.). But I try my best to do it.

My first attempt was to use Iterator

mutable references:

fn mutate<'a, A, I>(items: I) where I: Iterator<&'a mut A>

      

The problem is I need to repeat multiple times over elements. But to avoid aliasing mutable references, structures such as Slice

IterMut

, do not implement Clone

or RandomAccessIterator

. As far as I know, I cannot use the same iterator to re-access mutable references multiple times.

So I looked at the line IndexMut

. This seems to be what I want, but I cannot find another dash to indicate the length of the container. And the framework Slice

that implements IndexMut

restricts the validation of each access, which is undesirable.

So is there a way to do what I want? It would be nice to use Iterator

s, as I really want to iterate over mutable elements multiple times.

+3


source to share


1 answer


You can ask for more restrictions:



fn mutate<'a, A, I>(items: I) 
where I: Iterator<Item=&'a mut A>,
      I: ExactSizeIterator,
      I: RandomAccessIterator,
      I: Clone {

      

+1


source







All Articles