Background: I\'m creating an iterator that returns a reference to a slice &[T]
, but the data vector needs to remain immutable. The iterator cannot
Rust prevents you from having more than one alias to an object if one of them is a mutable alias.
Here, Windower::out_data
is a mutable alias to some slice, and you're trying to return an immutable alias to the same data from your next
method. In order for this to be safe, Rust must prevent you from being able to use Windower::out_data
for as long as the slice returned by next
is in scope. This means that the signature fn next(&'b mut self) -> Option<Self::Item>
is indeed required, which means you simply cannot implement Iterator
with your current implementation.