I've been trying to understand this post on Stack Overflow along with its accepted answer. The context is the well-known example of trying to use safe code to write
IterMut for a data structure wrapping a slice stored on the heap.
First of all, it's obvious why one can't have two mutable references to the same slice at the same time. However, the answer on this post says that, given the fact that the lifetime of the return value of
next() must be the same as the lifetime of the iterator, two calls would cause a violation. I'm confused by that in and of itself - there are lots of functions that I could write that would yield a mutable reference to some internal state, and the borrow checker wouldn't complain until I tried to hold onto two of them at the same time. So the first part of my question is what makes this a special case that cares about some particular possible usage.
That said, the actual compiler error obviously doesn't say anything about potential multiple calls to
next() - it talks about conflicting lifetimes, which makes more sense to me. What I don't understand is what the two conflicting lifetimes are. The compiler is saying it can't find a lifetime that satisfies both
'a from the iterator as well as the autoref in the
get_mut() function. The second part of my question is: why can't the compiler take the anonymous lifetime to be