# Confusing behavior of \`into\_iter\` on vector and array

**URL:** <https://users.rust-lang.org/t/confusing-behavior-of-into-iter-on-vector-and-array/59168>\
**Category:** uncategorized\
**Created:** [April 30, 2021, 9:18pm UTC](https://users.rust-lang.org/t/confusing-behavior-of-into-iter-on-vector-and-array/59168 "2021-04-30T21:18:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dclong](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dclong/32/18205_2.png) [@dclong](https://users.rust-lang.org/u/dclong)\
**Post date:** [April 30, 2021, 9:18pm UTC](https://users.rust-lang.org/t/confusing-behavior-of-into-iter-on-vector-and-array/59168/1 "2021-04-30T21:18:29Z")

</div>

As the screenshot below shows, the following code block works

```rust
let words = vec!["alpha", "beta", "gamma"];
let merged: String = words.into_iter().collect();
println!("{}", merged);

```

however, it doesn't work if `array` is used.

```rust
let words = ["alpha", "beta", "gamma"];
let merged: String = words.into_iter().collect();
println!("{}", merged);

```

 ![image](https://us1.discourse-cdn.com/flex019/uploads/rust_lang/original/3X/0/b/0bf05d3dd59ad4149c5213b4290cc372245d892f.png)

It seems to me that `words.into_iter()` generates an iterator of `&&str` (instead of `&str`). Can anyone explain the reason behind this?

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [April 30, 2021, 9:19pm UTC](https://users.rust-lang.org/t/confusing-behavior-of-into-iter-on-vector-and-array/59168/2 "2021-04-30T21:19:20Z")

</div>

Please post your code as text rather than an image.

---

<div class="post-metadata">

**Author:** ![skysch](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@skysch](https://users.rust-lang.org/u/skysch)\
**Post date:** [April 30, 2021, 9:33pm UTC](https://users.rust-lang.org/t/confusing-behavior-of-into-iter-on-vector-and-array/59168/3 "2021-04-30T21:33:04Z")

</div>

[`IntoIterator` for arrays are only implemented for slices.](https://doc.rust-lang.org/std/primitive.slice.html#impl-IntoIterator) You can only return references from a slice.

You can do this if you want to own the iterated items:

```rust
fn main() {
    let words = ["alpha", "beta", "gamma"];
    let merged: String = words.iter().cloned().collect();
    println!("{}", merged);
}

```

Alternatively, there is an owned array iterator API: [IntoIter in std::array - Rust](https://doc.rust-lang.org/std/array/struct.IntoIter.html). (Or see [this issue](https://github.com/rust-lang/rust/pull/84147) to see some more recent discussion of the status of this.)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [April 30, 2021, 9:57pm UTC](https://users.rust-lang.org/t/confusing-behavior-of-into-iter-on-vector-and-array/59168/4 "2021-04-30T21:57:36Z")

</div>

> [@dclong](#):
>
> It seems to me that `words.into_iter()` generates an iterator of `&&str` (instead of `&str` ). Can anyone explain the reason behind this?

So what actually happens on calling `words.into_iter()` is that [method resolution](https://doc.rust-lang.org/stable/reference/expressions/method-call-expr.html#method-call-expressions) will look for a type that implements `IntoIterator` (the trait that `into_iter()` belongs to). In particular, method resolution can do some auto-referencing, which means it will check `[&str; 3]` then `&[&str; 3]` and then `&mut [&str; 3]` for `IntoIterator` implementations. The first one that [implements it is `&[&str; 3]`](https://doc.rust-lang.org/1.51.0/std/primitive.array.html#impl-IntoIterator) with an `Item` type `&&str`. (Generically `&[T; N]` turns into an iterator with item type `&T`). There is, as @skysch already mentioned, some newer work on [providing an `IntoIterator` implementation for `[T; N]`, too (already on nightly)](https://doc.rust-lang.org/nightly/std/primitive.array.html#impl-IntoIterator). This will eventually change the behavior of your `words.into_iter()` call to make the situation more similar to the `Vec`.

One reason why it took a while to get such an implementation is that it involces const generics ([std::array::IntoIter](https://doc.rust-lang.org/nightly/std/array/struct.IntoIter.html) has a `N: usize` parameter). Also since this change has the potential of breaking lots of existing code, there’s been work to special-case method resolution so that your `words.into_iter()` example might actually only change in the upcoming Rust 2021 edition.

---

<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, 9:57pm UTC](https://users.rust-lang.org/t/confusing-behavior-of-into-iter-on-vector-and-array/59168/5 "2021-07-29T21:57:46Z")

</div>

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.
