Skip to content

Diagnostics: suggest using array::IntoIter::new to iterate over arrays #82602

Closed

Description

When following the const-generics beta MVP blog post we ran into this diagnostics error. The blog post specifically requests filing const-generics related diagnostics issues, so here goes!

  • Rust 1.51.0-beta.3 (2021-02-24)

Given the following code:

use std::array;
fn needs_vec(v: Vec<i32>) {
    // ...
}

let arr = [vec![0, 1], vec![1, 2, 3], vec![3]];
for elem in arr {
    needs_vec(elem);
}

The current output is:

error[E0277]: `[Vec<{integer}>; 3]` is not an iterator
 --> src/main.rs:8:17
  |
8 |     for elem in arr {
  |                 ^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
  |
  = help: the trait `Iterator` is not implemented for `[Vec<{integer}>; 3]`
  = note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
  = note: required because of the requirements on the impl of `IntoIterator` for `[Vec<{integer}>; 3]`
  = note: required by `into_iter`

Ideally the output should look like:

error[E0277]: `[Vec<{integer}>; 3]` is not an iterator
 --> src/main.rs:8:17
  |
8 |     for elem in arr {
  |                 ^^^ call `array::IntoIter::new(arr)` to iterate over the array
  |
  = help: the trait `Iterator` is not implemented for `[Vec<{integer}>; 3]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions