Skip to content

Better suggestion when attempting to iterate over array #36391

Closed
@frewsxcv

Description

@frewsxcv

Code

let a = [1, 2, 4, 5, 6];

for e in a {
    println!("{}", e);
}

Compiler error

error[E0277]: the trait bound `[{integer}; 5]: std::iter::Iterator` is not satisfied
 --> <anon>:6:5
  |
6 |     for e in a {
  |     ^ trait `[{integer}; 5]: std::iter::Iterator` not satisfied
  |
  = note: `[{integer}; 5]` is not an iterator; maybe try calling `.iter()` or a similar method
  = note: required by `std::iter::IntoIterator::into_iter`

error: aborting due to previous error

Suggestion

Instead of suggesting .iter(), it'd be cool if we cool if we could check if the borrowed type implements IntoIterator and then suggest prefixing with &. More concretely, in the example above, [T; 5] does not implement IntoIterator but &[T; 5] does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-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