Skip to content

Suggest .collect() when Expected: FromIterator<Item> and <Found as FromIterator<Item> #159493

Description

@estebank

Code

fn main() {
    let input = "lowercase";
    let uppercase: String = input.chars().map(|c| c.to_ascii_uppercase());
}

Current output

error[E0308]: mismatched types
  --> $DIR/string-from-char-iterator-without-collect.rs:10:29
   |
LL |     let uppercase: String = input.chars().map(|c| c.to_ascii_uppercase());
   |                    ------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `Map<Chars<'_>, {closure@...}>`
   |                    |
   |                    expected due to this
   |
   = note: expected struct `String`
              found struct `Map<Chars<'_>, {closure@$DIR/string-from-char-iterator-without-collect.rs:10:47: 10:50}>`

Desired output

error[E0308]: mismatched types
  --> $DIR/string-from-char-iterator-without-collect.rs:10:29
   |
LL |     let uppercase: String = input.chars().map(|c| c.to_ascii_uppercase());
   |                    ------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `Map<Chars<'_>, {closure@...}>`
   |                    |
   |                    expected due to this
   |
   = note: expected struct `String`
              found struct `Map<Chars<'_>, {closure@$DIR/string-from-char-iterator-without-collect.rs:10:47: 10:50}>`
help: you can collect the `Iterator<Item = char>` into a `String`
   |
LL |     let uppercase: String = input.chars().map(|c| c.to_ascii_uppercase()).collect();
   |                                                                          ++++++++++

Rationale and extra context

No response

Other cases

Rust Version

1.97

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-papercutDiagnostics: An error or lint that needs small tweaks.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