Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding a foreach-like iterator adapter #1070

Closed
steveklabnik opened this issue Apr 18, 2015 · 6 comments
Closed

Consider adding a foreach-like iterator adapter #1070

steveklabnik opened this issue Apr 18, 2015 · 6 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@steveklabnik
Copy link
Member

Proposed in #582 and #1064, the Iterator trait could have a .foreach method which drains the iterator immediately and runs a closure on each element to concisely do so without a for loop.

@gkoz
Copy link

gkoz commented Apr 18, 2015

Alternatively, there could just be a method to drain the iterator so you wouldn't have to use count or all for that.

@untitaker
Copy link
Contributor

Are there any situations where you wouldn't want to use a for-loop?

@steveklabnik
Copy link
Member Author

It looks much worse when you're working with a chain of iterators.

@ranma42
Copy link
Contributor

ranma42 commented Nov 18, 2015

I can think of two use cases where it might be convenient more convenient than a for:

  • if you just want to run a function you already have:
    for x in values {
        my_log(x)
    }
    values.foreach(my_log)
  • if you are doing a pipeline of iterators (it is very common in functional languages, AFAICT not so common in Rust).

@iliekturtles
Copy link

See here for reasoning (which I agree with) why the equivalent method wasn't included in .NET's LINQ.

@cuviper
Copy link
Member

cuviper commented Jun 30, 2017

Iterator::for_each() was merged in rust-lang/rust#42782, tracked in rust-lang/rust#42986.

@petrochenkov petrochenkov added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Jan 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

8 participants