# \[Pre-RFC\] Iter advance

**URL:** <https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398>\
**Category:** language design\
**Created:** [February 8, 2019, 10:05pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398 "2019-02-08T22:05:20Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 8, 2019, 10:05pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/1 "2019-02-08T22:05:20Z")

</div>

I have sometimes needed to skip the next n items in an iterator but there doesn’t seem to be able a way to do that besides calling `.next()` n times which seems really slow.

So I propose the following function:

```rust
fn advance_by(&mut self, n: usize) -> Self {
    ...
}

```

This function would skip over the next n items in the iterator. I haven’t look at the code so see if it is possible to do this without having to internally call `next()` but to make this useful I would say that it shouldn’t if at all possible.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [February 8, 2019, 10:43pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/2 "2019-02-08T22:43:53Z")

</div>

You can do this with [`nth()`](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.nth). By default that does just step through many calls to `next()`, but many iterator types implement it with something more optimized.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [February 8, 2019, 11:23pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/3 "2019-02-08T23:23:23Z")

</div>

If I understood correctly, `iter.skip(n).next()` should do this.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 9, 2019, 12:08am UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/4 "2019-02-09T00:08:09Z")

</div>

That won’t work because `skip` takes `self` not `&mut self`

Sebastian Malton

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [February 9, 2019, 12:51am UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/5 "2019-02-09T00:51:31Z")

</div>

`iter.by_ref().skip(n).next()`

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [February 9, 2019, 6:50am UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/6 "2019-02-09T06:50:49Z")

</div>

There’s also [`Itertools::dropping()`](https://docs.rs/itertools/0.8.0/itertools/trait.Itertools.html#method.dropping) which is like a non-lazy `skip()`.

---

<div class="post-metadata">

**Author:** ![dcarosone](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dcarosone/32/5244_2.png) [@dcarosone](https://internals.rust-lang.org/u/dcarosone)\
**Post date:** [February 9, 2019, 7:02am UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/7 "2019-02-09T07:02:55Z")

</div>

> [@Nokel81](#):
>
> That won’t work because `skip` takes `self` not `&mut self`

I'm not sure I see the problem; it takes ownership of the original iter and returns a wrapped version that does the skipping when first invoked (ie remains lazy).

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 9, 2019, 4:52pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/8 "2019-02-09T16:52:48Z")

</div>

If I am in a function where the iter is borrowed mutabily then I cannot use any function that takes ownership.

Sebastian Malton

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [February 9, 2019, 6:49pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/9 "2019-02-09T18:49:01Z")

</div>

> [@Nokel81](#):
>
> If I am in a function where the iter is borrowed mutabily then I cannot use any function that takes ownership.

As @CAD97 already pointed out, `&mut impl Iterator: Iterator`, so you _can_ use a function that takes ownership by passing ownership of the mutable reference.

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [February 9, 2019, 7:48pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/10 "2019-02-09T19:48:48Z")

</div>

Yep. See [this example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cbd9734780fb356099d43a674a693619):

```rust
let mut numbers = (0 .. 10).into_iter();
(&mut numbers).take(3).for_each(|x| {
  println!("{} is in the podium!", x);
});
numbers.for_each(|x| {
  println!("{} is not.", x)
})

```

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 9, 2019, 9:24pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/11 "2019-02-09T21:24:27Z")

</div>

Yes but that requires the for\_each() which means that it is not efficient.

[CAD97](https://internals.rust-lang.org/u/CAD97)’s `iter.by_ref().skip(n).next()` is equivalent to [cuviper](https://internals.rust-lang.org/u/cuviper)’s `iter.nth(n)` however the next `next()` call will return the n+2’nd item which is a bit counter intuitive.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [February 9, 2019, 9:58pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/12 "2019-02-09T21:58:18Z")

</div>

How is using `for_each` inefficient?

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [February 9, 2019, 10:05pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/13 "2019-02-09T22:05:58Z")

</div>

> [@Nokel81](#):
>
> Yes but that requires the for\_each() which means that it is not efficient.

It is not required at all, that was just my own coding style (when there is no `break`/early `return`); you can perfectly use `for x in (&mut iterator).take(3) { /* ... */ }`.

And, by the way, `it.for_each(|x| {` is easier for the compiler to optimize than `for x in it {`, which means that it is actually the one that may be more efficient.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 9, 2019, 10:18pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/14 "2019-02-09T22:18:19Z")

</div>

Yes I understand, but my use case is to just discard the skipped elements entirely. So it is more efficient to just advance through if possible then calling next a bunch

Sebastian Malton

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [February 9, 2019, 10:36pm UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/15 "2019-02-09T22:36:34Z")

</div>

## Skipping (the) `n` (first) elements of an `iterator`

- 
### lazily

`let mut iterator = iterator.skip(n);`

- 
### eagerly

`(&mut iterator).take(n).for_each(std::mem::drop);`

Aside: if `::std::mem::drop` makes your code too readable, you can always use its “implementation” instead: `|_| ()`

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [February 10, 2019, 2:06am UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/16 "2019-02-10T02:06:11Z")

</div>

`for_each` can be _faster_ than a plain `for` loop, because it allows internal iteration. Rather than repeating calls to `next()` that have to check state, some iterators can implement tighter inner loops. I had some benchmarks with `chain()` in [PR42782](https://github.com/rust-lang/rust/pull/42782):

> <https://github.com/rust-lang/rust/commit/4a8ddac99e1edfb219e11c3ea2d6c43ccecb29ab>

But I would still bet on `nth()` if all you’re going to do is drop it.

---

<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:** [May 11, 2019, 2:06am UTC](https://internals.rust-lang.org/t/pre-rfc-iter-advance/9398/17 "2019-05-11T02:06:12Z")

</div>

Closed on May 11, 2019, 2:06am UTC

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