So naturally, I wondered whether I could wrap this in an
Iterator interface. After reading up a little bit on streaming iterators, I realized it wouldn’t be so simple. Still, this answer on SO says that even though I can’t return a reference into the iterator struct itself from
.next(), I should be able to return a reference into some other structure (see paragraph beginning with “Update:”).
So I came up with this solution, where the
Iterator stores a mutable reference to a
String, reads data into it, and each call to
.next() returns that reference. But this complains about conflicting lifetimes (see messages if you try to compile the example).
Probably the most confusing part is the message (simplified)
expected Option<&'a String>, found Option<&String> – how is that possible? The type of the
line field of
MyLines is defined as
&'a mut String, so how can
self: MyLines) not be
I would be very grateful for any help in better understanding the problem and whether it’s fixable or not