The linked article contrasts them with the GoF-style iterators as well. The Rust Iterator trait is similar to the "one pass ranges" (and possibly forward ranges), but not double-ended ranges or random-access ranges. It's the *family* of range-based iterators that makes it flexible (e.g. allowing you to write an efficient in-place reverse without knowing the underlying data structure, using a double-ended range).
See fig. 3: http://www.informit.com/content/images/art_alexandrescu3_iterators/elementLinks/alexandrescu3_fig03.jpg On Thu, Jun 6, 2013 at 8:16 PM, Daniel Micay <[email protected]> wrote: > On Thu, Jun 6, 2013 at 11:01 PM, Sebastian Sylvan > <[email protected]> wrote: > > > > > > On Thu, Jun 6, 2013 at 7:22 PM, Bill Myers <[email protected]> > 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. > -- Sebastian Sylvan
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
