-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
compiler: suggest .collect() when String is expected and Iterator is found
#156121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 2 commits into
rust-lang:main
from
thiago-fealves:suggest-collect-string
May 18, 2026
+148
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| fn main() { | ||
| let _x: String = "hello".chars().map(|c| c); | ||
|
thiago-fealves marked this conversation as resolved.
|
||
| //~^ ERROR mismatched types | ||
| //~| HELP consider using `.collect()` to convert the `Iterator` into a `String` | ||
|
|
||
| let _y: Vec<i32> = vec![1, 2, 3].into_iter().map(|x| x); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP consider using `.collect()` to convert the `Iterator` into a `Vec<i32>` | ||
|
|
||
| let res: Result<Vec<i32>, _> = ["1", "2"].into_iter().map(|s| s.parse::<i32>()); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP consider using `.collect()` to convert the `Iterator` into a `Result<Vec<i32>, _>` | ||
| let (a, b): (Vec<i32>, Vec<i32>) = vec![1, 2].into_iter().map(|x| (x, x)); | ||
| //~^ ERROR mismatched types | ||
| //~| HELP consider using `.collect()` to convert the `Iterator` into a `(Vec<i32>, Vec<i32>)` | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-collect.rs:2:22 | ||
| | | ||
| LL | let _x: String = "hello".chars().map(|c| c); | ||
| | ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `Map<Chars<'_>, {closure@...}>` | ||
| | | | ||
| | expected due to this | ||
| | | ||
| = note: expected struct `String` | ||
| found struct `Map<Chars<'_>, {closure@$DIR/suggest-collect.rs:2:42: 2:45}>` | ||
| help: consider using `.collect()` to convert the `Iterator` into a `String` | ||
| | | ||
| LL | let _x: String = "hello".chars().map(|c| c).collect(); | ||
| | ++++++++++ | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-collect.rs:6:24 | ||
| | | ||
| LL | let _y: Vec<i32> = vec![1, 2, 3].into_iter().map(|x| x); | ||
| | -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Vec<i32>`, found `Map<IntoIter<{integer}>, {closure@...}>` | ||
| | | | ||
| | expected due to this | ||
| | | ||
| = note: expected struct `Vec<i32>` | ||
| found struct `Map<std::vec::IntoIter<{integer}>, {closure@$DIR/suggest-collect.rs:6:54: 6:57}>` | ||
| help: consider using `.collect()` to convert the `Iterator` into a `Vec<i32>` | ||
| | | ||
| LL | let _y: Vec<i32> = vec![1, 2, 3].into_iter().map(|x| x).collect(); | ||
| | ++++++++++ | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-collect.rs:10:36 | ||
| | | ||
| LL | let res: Result<Vec<i32>, _> = ["1", "2"].into_iter().map(|s| s.parse::<i32>()); | ||
| | ------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<Vec<i32>, _>`, found `Map<Iter<'_, &str>, {closure@...}>` | ||
| | | | ||
| | expected due to this | ||
| | | ||
| = note: expected enum `Result<Vec<i32>, _>` | ||
| found struct `Map<std::slice::Iter<'_, &str>, {closure@$DIR/suggest-collect.rs:10:63: 10:66}>` | ||
| help: consider using `.collect()` to convert the `Iterator` into a `Result<Vec<i32>, _>` | ||
| | | ||
| LL | let res: Result<Vec<i32>, _> = ["1", "2"].into_iter().map(|s| s.parse::<i32>()).collect(); | ||
| | ++++++++++ | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-collect.rs:13:40 | ||
| | | ||
| LL | let (a, b): (Vec<i32>, Vec<i32>) = vec![1, 2].into_iter().map(|x| (x, x)); | ||
| | -------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `(Vec<i32>, Vec<i32>)`, found `Map<IntoIter<{integer}>, {closure@...}>` | ||
| | | | ||
| | expected due to this | ||
| | | ||
| = note: expected tuple `(Vec<i32>, Vec<i32>)` | ||
| found struct `Map<std::vec::IntoIter<{integer}>, {closure@$DIR/suggest-collect.rs:13:67: 13:70}>` | ||
| help: consider using `.collect()` to convert the `Iterator` into a `(Vec<i32>, Vec<i32>)` | ||
| | | ||
| LL | let (a, b): (Vec<i32>, Vec<i32>) = vec![1, 2].into_iter().map(|x| (x, x)).collect(); | ||
| | ++++++++++ | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0308`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.