A specific solution to my specific problem is appreciated.
And I'd also like to learn a bit more generally
I'm fairly new to Rust, but have read the Book and many examples for Iterators.
I am aware many explanations and tutorials exist.
I have found that most explanations/tutorials for Iterators have one (often simplistic) example for which they show how to implement an iterator. For most of the steps or syntax, they don't really explain why they are necessary.
Being pretty experienced in C++ and having only a semi grasp on Rust lifetime (notations), I found it hard to write my own Iterators, because the mental model is so different in Rust.
It appears my problems never fit the simplistic tutorials or the examples were so specific that I cannot parse out the important things.
In my attempts I have often found myself in a situation where I have a working iterator adaptor chain such as
[o.a.map(|v| Options::A(v)), o.b.clone().map(|v| Options::B(v))] .into_iter() .flatten()
which I now want to shape into an iterator for
From what I have read and understand, the following factors are important for the solution of the implementation:
- Do you introduce new "state" (here the array
- Do you want to consume or borrow?
- Are you ok with a
I also know about some tools at my disposal, which I understand in isolation:
But I don't know how to put the pieces together to reliably get from an iterator chain like above to my own iterator, because iterator types change on each chain step and some borrow and some consume.
Do you have some guidance/ resources on how to go about converting an iterator adaptor chain into an iterator implementation?
I am aware this is kinda an open ended question, which is why this is not on Stackoverflow.
I think a mapping from "if you want this" use "IntoIterator", "if you want this", use "Iterator" would be helpful.
I think I am lacking a mental model for when iterator adaptors (
collect) are borrowing and when they are consuming.
To kickstart the conversation, I have the above example in a playground with a missing iterator implementation:
Many thanks in advance.