{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/42782","repository_url":"https://api.github.com/repos/rust-lang/rust","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/42782/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/42782/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/42782/events","html_url":"https://github.com/rust-lang/rust/pull/42782","id":237365324,"number":42782,"title":"Add `Iterator::for_each`","user":{"login":"cuviper","id":36186,"avatar_url":"https://avatars1.githubusercontent.com/u/36186?v=4","gravatar_id":"","url":"https://api.github.com/users/cuviper","html_url":"https://github.com/cuviper","followers_url":"https://api.github.com/users/cuviper/followers","following_url":"https://api.github.com/users/cuviper/following{/other_user}","gists_url":"https://api.github.com/users/cuviper/gists{/gist_id}","starred_url":"https://api.github.com/users/cuviper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cuviper/subscriptions","organizations_url":"https://api.github.com/users/cuviper/orgs","repos_url":"https://api.github.com/users/cuviper/repos","events_url":"https://api.github.com/users/cuviper/events{/privacy}","received_events_url":"https://api.github.com/users/cuviper/received_events","type":"User","site_admin":false},"labels":[{"id":583426710,"url":"https://api.github.com/repos/rust-lang/rust/labels/S-waiting-on-review","name":"S-waiting-on-review","color":"d3dddd","default":false},{"id":211668062,"url":"https://api.github.com/repos/rust-lang/rust/labels/T-libs","name":"T-libs","color":"bfd4f2","default":false}],"state":"closed","locked":false,"assignee":{"login":"alexcrichton","id":64996,"avatar_url":"https://avatars1.githubusercontent.com/u/64996?v=4","gravatar_id":"","url":"https://api.github.com/users/alexcrichton","html_url":"https://github.com/alexcrichton","followers_url":"https://api.github.com/users/alexcrichton/followers","following_url":"https://api.github.com/users/alexcrichton/following{/other_user}","gists_url":"https://api.github.com/users/alexcrichton/gists{/gist_id}","starred_url":"https://api.github.com/users/alexcrichton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexcrichton/subscriptions","organizations_url":"https://api.github.com/users/alexcrichton/orgs","repos_url":"https://api.github.com/users/alexcrichton/repos","events_url":"https://api.github.com/users/alexcrichton/events{/privacy}","received_events_url":"https://api.github.com/users/alexcrichton/received_events","type":"User","site_admin":false},"assignees":[{"login":"alexcrichton","id":64996,"avatar_url":"https://avatars1.githubusercontent.com/u/64996?v=4","gravatar_id":"","url":"https://api.github.com/users/alexcrichton","html_url":"https://github.com/alexcrichton","followers_url":"https://api.github.com/users/alexcrichton/followers","following_url":"https://api.github.com/users/alexcrichton/following{/other_user}","gists_url":"https://api.github.com/users/alexcrichton/gists{/gist_id}","starred_url":"https://api.github.com/users/alexcrichton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexcrichton/subscriptions","organizations_url":"https://api.github.com/users/alexcrichton/orgs","repos_url":"https://api.github.com/users/alexcrichton/repos","events_url":"https://api.github.com/users/alexcrichton/events{/privacy}","received_events_url":"https://api.github.com/users/alexcrichton/received_events","type":"User","site_admin":false}],"milestone":null,"comments":25,"created_at":"2017-06-20T22:39:04Z","updated_at":"2018-04-02T18:12:35Z","closed_at":"2017-06-30T11:42:11Z","author_association":"MEMBER","pull_request":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/42782","html_url":"https://github.com/rust-lang/rust/pull/42782","diff_url":"https://github.com/rust-lang/rust/pull/42782.diff","patch_url":"https://github.com/rust-lang/rust/pull/42782.patch"},"body":"This works like a `for` loop in functional style, applying a closure to\r\nevery item in the `Iterator`.  It doesn't allow `break`/`continue` like\r\na `for` loop, nor any other control flow outside the closure, but it may\r\nbe a more legible style for tying up the end of a long iterator chain.\r\n\r\nThis was tried before in #14911, but nobody made the case for using it\r\nwith longer iterators.  There was also `Iterator::advance` at that time\r\nwhich was more capable than `for_each`, but that no longer exists.\r\n\r\nThe `itertools` crate has `Itertools::foreach` with the same behavior,\r\nbut thankfully the names won't collide.  The `rayon` crate also has a\r\n`ParallelIterator::for_each` where simple `for` loops aren't possible.\r\n\r\n> I really wish we had `for_each` on seq iterators. Having to use a\r\n> dummy operation is annoying.  - [@nikomatsakis][1]\r\n\r\n[1]: https://github.com/nikomatsakis/rayon/pull/367#issuecomment-308455185"},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/377998345","html_url":"https://github.com/rust-lang/rust/pull/42782#issuecomment-377998345","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/42782","id":377998345,"user":{"login":"scottmcm","id":18526288,"avatar_url":"https://avatars2.githubusercontent.com/u/18526288?v=4","gravatar_id":"","url":"https://api.github.com/users/scottmcm","html_url":"https://github.com/scottmcm","followers_url":"https://api.github.com/users/scottmcm/followers","following_url":"https://api.github.com/users/scottmcm/following{/other_user}","gists_url":"https://api.github.com/users/scottmcm/gists{/gist_id}","starred_url":"https://api.github.com/users/scottmcm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scottmcm/subscriptions","organizations_url":"https://api.github.com/users/scottmcm/orgs","repos_url":"https://api.github.com/users/scottmcm/repos","events_url":"https://api.github.com/users/scottmcm/events{/privacy}","received_events_url":"https://api.github.com/users/scottmcm/received_events","type":"User","site_admin":false},"created_at":"2018-04-02T18:12:35Z","updated_at":"2018-04-02T18:12:35Z","author_association":"MEMBER","body":"@phimuemue As an example of \"break with a certain item\", check out how `find` is implemented:\r\n\r\nhttps://github.com/rust-lang/rust/blob/ab8b961677ac5c74762dcea955aa0ff4d7fe4915/src/libcore/iter/iterator.rs#L1738-L1746\r\n\r\n(That `LoopState` type is currently internal, but personally I'd like `Try` to use it, as @cuviper said.)\r\n\r\nIf you wanted to do the same in your code today†, it can be done with `Result` like this:\r\n\r\n```rust\r\n    self.try_for_each(move |x| { \r\n        if predicate(&x) { Err(x) } \r\n        else { Ok(()) } \r\n    }).err() \r\n```\r\n\r\n† Well, once someone makes a stabilization PR for `try_for_each`..."}}
{"actor":{"display_login":"scottmcm"}}