Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One thing of roughly this nature is that a lot of rust code that I have read have their own types for iterators of objects.

For example, in the git2 crate there is both:

https://docs.rs/git2/0.13.12/git2/struct.Branch.html

and

https://docs.rs/git2/0.13.12/git2/struct.Branches.html

Where Branches implements Iterator.



That seems pretty natural? The only reason why you don't have this in e.g. python is that iterators are often implemented as generators, so the "own type of iterator" is implicit. But I remember pretty much the same thing from my Java days, if you need to iterate you have to create an iterator type. It's a bit less visible due to type erasure but it's there nonetheless.


Yeah it's not intuitive, but there isn't really another way to do it, right? Every concrete iterator is specific to what it returns and what it iterates over.

I think it's documented under something like "How to implement Iterator" but if that's not where people will go looking when they need it, then it should be somewhere else.


In C# and Python you can write an iterator using `yield` keyword. E.g.

   for(i in array) yield i
The language then transforms this into an implementation of the base iterator interface under the hood.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: