# Add .foreach method to Iterators for side effects

**URL:** <https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312>\
**Category:** Uncategorized\
**Created:** [January 13, 2015, 4:31pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312 "2015-01-13T16:31:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![larroy](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@larroy](https://internals.rust-lang.org/u/larroy)\
**Post date:** [January 13, 2015, 4:31pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/1 "2015-01-13T16:31:37Z")

</div>

Using .map for side effects and not using the result might give warnings, and it's not good practice to do side effects on map. Adding .foreach sugar clearly states the intention of performing an action just for the side effects.

It was suggested on IRC to discuss this change here. It has also been discussed on reddit:

> **[r/rust - Does having foreach implemented for iterators seem desirable?](https://www.reddit.com/r/rust/comments/2s5jjs/does_having_foreach_implemented_for_iterators/)**
>
> 7 votes and 18 comments so far on Reddit

See the related pull request:

[https://github.com/rust-lang/rust/pull/21098](https://github.com/rust-lang/rust/pull/21098)

---

<div class="post-metadata">

**Author:** ![steveklabnik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steveklabnik/32/4524_2.png) [@steveklabnik](https://internals.rust-lang.org/u/steveklabnik)\
**Post date:** [January 13, 2015, 4:34pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/2 "2015-01-13T16:34:55Z")

</div>

I’m a big fan of this. I don’t like having to have a variable to save the iterator and then switch to the for-style loop when I have big chains of iterator adapters.

---

<div class="post-metadata">

**Author:** ![nagisa](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nagisa/32/2210_2.png) [@nagisa](https://internals.rust-lang.org/u/nagisa)\
**Post date:** [January 13, 2015, 5:06pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/3 "2015-01-13T17:06:26Z")

</div>

I’m not against it if and only if `iterator.foreach(cb).filter(another_cb).foreach(last_cb)` is made possible by this. `foreach` should be something like `map(|f| { foreach_cb(f); f})`, but eagerly-evaluated.

As far as eager evaluation goes it, it must be documented very clearly that this is exception to convention of iterators being lazy.

EDIT: obviously this also means there’s an implicit list of intermediate values at the boundary of each foreach.

---

<div class="post-metadata">

**Author:** ![phaylon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/phaylon/32/62_2.png) [@phaylon](https://internals.rust-lang.org/u/phaylon)\
**Post date:** [January 13, 2015, 5:30pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/4 "2015-01-13T17:30:38Z")

</div>

I wonder if a name like `into` instead of `foreach` would make it a bit clearer that it’s a consuming, evaluating method. I’d love to have something like this, even though I’m unsure about what a clear name would be.

An `into` could possibly also be more useful than just eager iteration of a closure. It could take anything with an `IteratorReceiver` trait as argument. Some examples:

```
# the general use-case
range(0, 100).into(|n| println!("n {}", n));

# populating a Vec
let mut v = vec![1, 2, 3];
range(20, 50).into(&mut v);
range(100, 200).into(&mut v);

```

Channels might also be sane receivers.

Unsure if this could be done without requiring the closure to be passed as `&mut |n| ...` implicitly.

---

<div class="post-metadata">

**Author:** ![steveklabnik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steveklabnik/32/4524_2.png) [@steveklabnik](https://internals.rust-lang.org/u/steveklabnik)\
**Post date:** [January 13, 2015, 5:40pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/5 "2015-01-13T17:40:57Z")

</div>

I would actually very much want `foreach()` to _not_ be an adapter, but be more like `collect()`. Currently, all iterators are lazy, this would break that rule.

---

<div class="post-metadata">

**Author:** ![nagisa](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nagisa/32/2210_2.png) [@nagisa](https://internals.rust-lang.org/u/nagisa)\
**Post date:** [January 13, 2015, 5:49pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/6 "2015-01-13T17:49:35Z")

</div>

In that case

```rust
.fold((), |_, x|{ 
    statement1;
    statement2;
})

```

is equivalent to the proposed method.

---

<div class="post-metadata">

**Author:** ![nagisa](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nagisa/32/2210_2.png) [@nagisa](https://internals.rust-lang.org/u/nagisa)\
**Post date:** [January 18, 2015, 11:19pm UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/7 "2015-01-18T23:19:29Z")

</div>

This has a [RFC](https://github.com/rust-lang/rfcs/pull/582).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:23am UTC](https://internals.rust-lang.org/t/add-foreach-method-to-iterators-for-side-effects/1312/8 "2019-03-25T08:23:38Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
