hey, Iterators are all abundent in the standard library. But I'm having strangely hard time finding any structs implementing it. I want something like lazy generator, not like Vector, so basically Iterator, but not trait Iterator instead:
struct Iterator { next: Box<Fn blabla> }
I wouldn't mind defining this exact type in my code if only I could inline the definition inside function definition. I don't want to pass generic dynamic Iterator, because this introduces needless to me type variable, complicating things.
Something that generates iterated elements out of thin air but keeps them inside. That's currently not possible in Rust. But Range creates objects and then gives them away. That is possible and easy.
If you're looking for an implementation of Iterator that is “concrete” in that you construct it with some items and those are the items it produces, then you want the Vec iterator (which is of type std::vec::IntoIter):
vec![1, 2, 3].into_iter()
If that's not it, some more description (preferably with code) of how you want to use what you're looking for would help.
Hey, thanks the explicit list of implementators is what I was looking for. from_fn is usefull as well.
But dyn Iterator is generic, isn't it? Having it as a struct field introduces a type parameter and this is precisely what I wanted to avoid. I'm thinking if I keep in the struct and I'm going to use single implementation in the end, then really I could just be using concrete Iterator provider.
This is one of those confusing situations where a term has one meaning as technical jargon and a different one in casual speech. When talking about Rust, "generic" usually refers to something that takes a type parameter, e.g. Something<T>, which gets resolved to a specific type at compile time. dyn Iterator is a mechanism for dynamic dispatch, which lets the backing type be determined at runtime— This is "generic" in the casual sense, in that it's code that can work with multiple types, but not in the jargon sense (it uses a completely different mechanism).
That is a concrete struct not abstract dyn Iterator, yet it's exactly the same expressive. My reason was that I wanted to include such an Iterator in a struct. Out of the 3 below options I like the third best:
struct A(x: Box<dyn Iterator>)
struct A<T: Iterator>(x: T) // weird that this even works? but cool
In order to make this compile, you will need a type parameter for the function too:
struct A<F> { x: FromFn<F> }
If that is acceptable to you, then you can use a parameter for the iterator, as in your second option, and that is strictly more flexible than requiring the use of FromFn. [1]