On Thu, Jun 6, 2013 at 11:01 PM, Sebastian Sylvan <sebastian.syl...@gmail.com> wrote: > > > On Thu, Jun 6, 2013 at 7:22 PM, Bill Myers <bill_my...@outlook.com> wrote: >> >> Scala has a similar design, with the following traits: >> - TraversableOnce: can be internally iterated once (has a foreach() method >> that takes a closure) >> - Traversable: can be internally iterated unlimited times (has a foreach() >> method that takes a closure) >> - Iterable: can be externally iterated (has an iterator() method that >> returns an Iterator trait) >> >> The way it works is that Iterable extends Traversable, which extends >> TraversableOnce, and the for loop just uses TraversableOnce, and Iterable >> has a default implementation of the TraversableOnce foreach() function using >> the iterator() function. >> >> Also, the Iterator trait itself extends TraversableOnce, implementing >> foreach() by mutating itself. >> >> It might be a good idea to investigate copying this design. > > > I find Andrei Alexandrescu's argument about range-based iterators pretty > persuasive: http://www.informit.com/articles/article.aspx?p=1407357 > > Seb
Andrei usually talks about ranges in contrast with C++ iterators, so it's a bit different than comparing with the Iterator trait Rust is using. C++ iterators don't know when they've reached the end of the range, so you have to compare them with a sentinel end iterator. In that sense, Rust's iterator is a range and the `next` method is comparable to the pop method on D ranges. _______________________________________________ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev