a bit of respite

010110__010110

#Iterator Blocks for Rust

Lately, I’ve been thinking about what it would take to implement C#-inspired iterator blocks for Rust―naturally something that has been discussed before―and decided to try to bring some order into my thought process by means of this blog.

</img>

Iterator blocks (in C# parlance) or generators (Python) are what is commonly associated with a yield statement or derivations thereof. The idea behind them is to allow the programmer to write something that looks like code typically found in the context of internal iteration, but then automatically transform this code into something that has the properties of an external iterator.

##Why would one want to do that?

Bob Nystrom has a fine article on external vs internal iteration and their various advantages and disadvantages (also interesting: Yield: Mainstream Delimited Continuations). The gist of it is that internal iterators for complex (e.g. tree- or graph-like) data structures are often easier to write because one can use the callstack to keep track of the iteration state involved. On the other hand external iterators have some nice properties:

Iterator blocks allow to have both advantages at the same time:

So they are, hands down, a good thing. However, yield means slightly different things in C#, Ruby, Javascript, Python, F#, Sather, and CLU. And it will certainly mean something slightly different from all of the above in Rust too. So over the next few weeks I plan to explore with some depth what the specific circumstances of an iterator block implementation for Rust are. This will probably include the following topics:

Of course I’ll try to also discuss each of these topics on the Rust mailing list. No use in soliloquizing about something that needs broad acceptance by the community. In the best case scenario this might even turn into a proof-of-concept implementation some time in the future (no promises made though). I’d already be happy to have a few interested readers.