# Using Iterators.rest to iterate over a range

**URL:** https://discourse.julialang.org/t/using-iterators-rest-to-iterate-over-a-range/42597
**Category:** General Usage
**Created:** [July 6, 2020, 8:55am UTC](https://discourse.julialang.org/t/using-iterators-rest-to-iterate-over-a-range/42597 "2020-07-06T08:55:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![andrewellis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewellis/32/97_2.png) [@andrewellis](https://discourse.julialang.org/u/andrewellis)
#### Post date: [July 6, 2020, 8:55am UTC](https://discourse.julialang.org/t/using-iterators-rest-to-iterate-over-a-range/42597/1 "2020-07-06T08:55:17Z")

</div>

Can anyone explain why

```julia
Iterators.rest([1, 2, 3], 2) |> collect

```

results in

```julia
2-element Array{Int64,1}:
 2
 3

```

but

```julia
Iterators.rest(1:3, 2) |> collect

```

returns

```julia
1-element Array{Int64,1}:
 3

```

?

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [July 6, 2020, 10:15am UTC](https://discourse.julialang.org/t/using-iterators-rest-to-iterate-over-a-range/42597/2 "2020-07-06T10:15:13Z")

</div>

It is because `Array` and `UnitRange` handle their iteration state differently. For `Array`, the returned state is the _index of the next element_, for `UnitRange`, it is the _value of the current element_ (ranges do arithmetic on their values for iteration, their indices are not used):

```julia
julia> iterate([1,2,3])
(1, 2)

julia> iterate(1:3)
(1, 1)

julia> iterate([4,5,6])
(4, 2)

julia> iterate(4:6)
(4, 4)

```

Since the second argument to `Iterators.rest` is the iteration state, this is not portable between `Array`s and `UnitRange`s.

---

<div class="post-metadata">

### Author: ![andrewellis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewellis/32/97_2.png) [@andrewellis](https://discourse.julialang.org/u/andrewellis)
#### Post date: [July 6, 2020, 10:25am UTC](https://discourse.julialang.org/t/using-iterators-rest-to-iterate-over-a-range/42597/3 "2020-07-06T10:25:09Z")

</div>

great, thanks!

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [July 6, 2020, 10:48am UTC](https://discourse.julialang.org/t/using-iterators-rest-to-iterate-over-a-range/42597/4 "2020-07-06T10:48:41Z")

</div>

FYI: `Iterators.drop` will skip a specified number of elements at the start of an iterator, regardless of how the iteration is implemented internally.
