3 ms·
Ack, what bad timing. Rust is in the middle of switching its dominant iteration idiom from internal iterators to external iterators. See also: https://github.c
by kibwen 13y ago
Ack, what bad timing. Rust is in the middle of switching its dominant iteration idiom from internal iterators to external iterators. See also:
https://github.com/mozilla/rust/wiki/Doc-detailed-release-notes#iterators https://github.com/mozilla/rust/wiki/Doc-detailed-release-no...
http://journal.stuffwithstuff.com/2013/01/13/iteration-inside-and-out/ http://journal.stuffwithstuff.com/2013/01/13/iteration-insid...
The `for` construct will be changing in the 0.8 cycle to reflect this change. Currently it looks like:
for [1,2,3].each |i| { /* a closure */ }
Where eventually it might look something like:
for i in [1,2,3] { /* just a regular block */ }
I'm actually surprised that the author praises the "freedom and flexibility" of our old `for` loops... the reason for making this switch at all is because we found our old semantics to be neither sufficiently composable nor sufficiently flexible! :)
Hopefully the author will revisit this space once the new implementation is finalized, perhaps with a comparison to D this time.
TL;DR: Rust is switching idioms from Ruby-style iteration to C#-style iteration because (for our purposes) it's more performant, easier to optimize, more composable, and more flexible.
- chrismorgan 13y agoWell, in 0.7 the external iterator `each` pattern is mostly gone, leaving external iterators the standard style, but because the semantics of `for` haven't changed yet, there is an `advance` method provided for iterators, such that the code is actually at present thus: for [1, 2, 3].iter().advance |i| { /* a closure */ }
- kibwen 13y agoRight, I was hoping we could avoid mentioning this brief and ugly interlude in our evolution. :)
- bjz_ 13y agoIt's been a courageous effort by strcat, pushing such ugliness on us over the 0.7 milestone, but it will certainly pay off into the future. I look forward to the new sugar!
- anonymous 13y agoThat is basically a while loop without a "proper body", right? At each iteration advance is called with the closure as first argument and iteration continues until it returns false?
- kibwen 13y agoYou're correct. You can see as much here in the implementation of `advance` (as you'd expect, it's very simple): https://github.com/mozilla/rust/blob/master/src/libstd/iterator.rs#L472 https://github.com/mozilla/rust/blob/master/src/libstd/itera...