Skip to content

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

Closed
@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

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

    No branches or pull requests

    Issue actions