Iter<T> is there a way to build a
Iter<&T>? I've looked at https://doc.rust-lang.org/std/slice/struct.Iter.html but see nothing obvious.
No*, that would require some sort of streaming iterator.
* you can collect the items into a
Vec<T>, then call
iter to get an iterator over
For my mental models, can we construct a situation where bad things happen if
Iter<T> -> Iter<&T> exited for arbitrary
My mental intuition for why this should be safe is:
Imagine A is the consumer of
Here's what B can do. B takes a
Iter<T>, consumes an element on demand, and passes a ref to A.
Then, when A wants the next elem, B frees up the old one ... hmm <-- is this where this beaks down? B can't know when to "free" the existing element -- so we end up with either (1) memory leak due to keeping all the T's around or (2) potentially unsafe/dangling ref's ?
Remember that for all iterators this is valid and must be supported:
let a = iter.next(); let b = iter.next(); drop(iter); use_both(a, b);
Exactly, you can never be sure when to free them in the normal iterator interface. With streaming iterators, you can free them on the next element, but not with normal iterators.
Yep, just like