This question is related, however it moreso covers the reason why the compiler cannot infer a safe lifetime when returning a mutable reference from Iterator::next
, which I think I understand.
My question is:
What are the specific steps you can take when designing your own iterator so that it can produce mutable references? Ultimately, I'm hoping for a concise-as-possible, step-by-step, commented example of both an Iterator
and its next
implementation that I (and anyone) can go to as a clear reference when they run into this situation. unsafe
examples are fine, I imagine they are probably necessary!
NOTE: I understand that MutItems
is normally the recommended example, however its implementation can be difficult to follow as there isn't any documentation on 1. How the markers work in that situation and 2. What the iterator!
macro expands to and how it works. If you use MutItems
as your example could you please clarify these things?