Skip to content

Better error message when forgetting iter() on slices  #94581

@edhebi

Description

@edhebi

Given the following code: https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=965472aeea871e8f49d3decac84abe64

fn get_slice() -> &'static [i32] {
    &[1, 2, 3, 4]
}

fn main() {
    let sqsum = get_slice().map(|i| i * i).sum();
}

The current output is:

error [E0599]: the method `map` exists for reference `&'static [i32]`, but its trait bounds were not satisfied
 --> src/main.rs:6:29
  |
6 |     let sqsum = get_slice().map(|i| i * i).sum();
  |                             ^^^ method cannot be called on `&'static [i32]` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `&'static [i32]: Iterator`
          which is required by `&mut &'static [i32]: Iterator`
          `[i32]: Iterator`
          which is required by `&mut [i32]: Iterator`

Ideally the output should at least mention that .iter() exists on slices

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 3, 2022
added
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
D-papercutDiagnostics: An error or lint that needs small tweaks.
D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
on Mar 5, 2022
added a commit that references this issue on Mar 9, 2022
ef6136e
added a commit that references this issue on Mar 9, 2022
32d7f81
added a commit that references this issue on Mar 10, 2022
e7281d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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

      Participants

      @estebank@edhebi

      Issue actions

        Better error message when forgetting `iter()` on slices · Issue #94581 · rust-lang/rust