# Flat mapping arrays

**URL:** <https://users.rust-lang.org/t/flat-mapping-arrays/59142>\
**Category:** uncategorized\
**Created:** [April 30, 2021, 1:51pm UTC](https://users.rust-lang.org/t/flat-mapping-arrays/59142 "2021-04-30T13:51:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![derekdreery](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/derekdreery/32/4788_2.png) [@derekdreery](https://users.rust-lang.org/u/derekdreery)\
**Post date:** [April 30, 2021, 1:51pm UTC](https://users.rust-lang.org/t/flat-mapping-arrays/59142/1 "2021-04-30T13:51:25Z")

</div>

I've got an array of 3 elements, and I want to create an iterator that cycles the elements a number of times. I tried the following code (included to show my intention):

```rust
let arr = [1, 2, 3];
iter::repeat(arr).take(10).flatten()

```

This doesn't work because `array` does not implement `Iterator`. I can't make an iterator by borrowing in a `flat_map` because I create a temporary borrow that would have to live beyond the function. The following does work:

```rust
iter::repeat(&arr).take(10).flatten()

```

but this relies on the optimizer being able to propagate the constant nature of the length of &arr. Are there any better solutions?

---

<div class="post-metadata">

**Author:** ![trentj](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/trentj/32/5690_2.png) [@trentj](https://users.rust-lang.org/u/trentj)\
**Post date:** [April 30, 2021, 2:13pm UTC](https://users.rust-lang.org/t/flat-mapping-arrays/59142/2 "2021-04-30T14:13:47Z")

</div>

You can use `Iterator::cycle`:

```
arr.iter().cycle().take(10)

```

> [@derekdreery](#):
>
> but this relies on the optimizer being able to propagate the constant nature of the length of &arr.

Are you referring to the fact that `<&[T; N] as IntoIterator>::into_iter` just delegates to `[T]::iter`, so `N` is erased?

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/derekdreery/32/4788_2.png) [@derekdreery](https://users.rust-lang.org/u/derekdreery)\
**Post date:** [April 30, 2021, 2:17pm UTC](https://users.rust-lang.org/t/flat-mapping-arrays/59142/3 "2021-04-30T14:17:54Z")

</div>

> [@trentj](#):
>
> Are you referring to the fact that `<&[T; N] as IntoIterator>::into_iter` just delegates to `[T]::iter` , so `N` is erased?

Yeah exactly, the array is reinterpreted as a slice. It might make no difference to the asm though, I haven't looked. `arr.iter()` does the same thing.

---

<div class="post-metadata">

**Author:** ![trentj](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/trentj/32/5690_2.png) [@trentj](https://users.rust-lang.org/u/trentj)\
**Post date:** [April 30, 2021, 2:21pm UTC](https://users.rust-lang.org/t/flat-mapping-arrays/59142/4 "2021-04-30T14:21:48Z")

</div>

(Off the top of my head) That seems unlikely to be a major performance issue even if the optimizer can't see through it; however, you raise an interesting point. It might be nice to have `array::Iter(Mut)<T, N>` (or whatever) instead of just delegating to the slice versions. Doing so would certainly break backwards compatibility, though, similar to `array::IntoIter`.

Speaking of which, you could use `array::IntoIter`:

```
std::array::IntoIter::new(arr).cycle().take(10)

```

However, since `Cycle` has to copy the whole struct, that might be worse if the optimizer is better at propagating constants than eliminating copies. Probably test it.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cuviper/32/3156_2.png) [@cuviper](https://users.rust-lang.org/u/cuviper)\
**Post date:** [April 30, 2021, 2:23pm UTC](https://users.rust-lang.org/t/flat-mapping-arrays/59142/5 "2021-04-30T14:23:43Z")

</div>

`IntoIterator for [T; N]` is coming in 1.53, but until then `std::array::IntoIter` does the same thing.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 29, 2021, 2:24pm UTC](https://users.rust-lang.org/t/flat-mapping-arrays/59142/6 "2021-07-29T14:24:37Z")

</div>

Closed on July 29, 2021, 2:24pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
