Overzealous borrow-checker (IMHO)

Yeah, this has been discussed to death and the two main camps seem to be:

  • It would be better named uniq or some other name, but it's impossible to change now
  • mut gives the right intuition most of the time and it's harder to explain uniqueness than mutation, so it's best the way it is.

This is a slight mischaracterization. You can access the data while iterating, you just have to do it through the exclusive borrow taken when you called chunks_mut. You can't do an end-run around an exclusive borrow by using a different reference to the original data; that would defeat the whole point. But you can access the chunk currently being iterated over, and you can access chunks from previous iterations, if you save them.

It's really a fundamental property of iterators: they only give you one thing at a time. In other languages, you may sometimes ignore this by accessing data without going through the iterator. The code you wrote would work translated into C++, for instance. But then C++ allows you to invalidate iterators, leading to data races and memory unsafety, precisely because it has nothing analogous to &mut that would stop you.

1 Like