]> git.lizzy.rs Git - rust.git/commit
Add `Iterator::for_each`
authorJosh Stone <jistone@redhat.com>
Tue, 20 Jun 2017 22:25:51 +0000 (15:25 -0700)
committerJosh Stone <jistone@redhat.com>
Tue, 20 Jun 2017 22:25:51 +0000 (15:25 -0700)
commitb4038977a39f7c5bfa76cccf586930ec57befbad
treeb93b87a762f7ea17657804ec3653b374bb0c7d64
parent29bce6e220f6fd2292d13d65fe503af7bf4852b7
Add `Iterator::for_each`

This works like a `for` loop in functional style, applying a closure to
every item in the `Iterator`.  It doesn't allow `break`/`continue` like
a `for` loop, nor any other control flow outside the closure, but it may
be a more legible style for tying up the end of a long iterator chain.

This was tried before in #14911, but nobody made the case for using it
with longer iterators.  There was also `Iterator::advance` at that time
which was more capable than `for_each`, but that no longer exists.

The `itertools` crate has `Itertools::foreach` with the same behavior,
but thankfully the names won't collide.  The `rayon` crate also has a
`ParallelIterator::for_each` where simple `for` loops aren't possible.

> I really wish we had `for_each` on seq iterators. Having to use a
> dummy operation is annoying.  - [@nikomatsakis][1]

[1]: https://github.com/nikomatsakis/rayon/pull/367#issuecomment-308455185
src/doc/unstable-book/src/library-features/iterator-for-each.md [new file with mode: 0644]
src/libcore/iter/iterator.rs