Skip to content
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

suggest .into_iter() when trying to collect Vec<T> into Vec<T> #82848

Open
matthiaskrgr opened this issue Mar 6, 2021 · 1 comment
Open

suggest .into_iter() when trying to collect Vec<T> into Vec<T> #82848

matthiaskrgr opened this issue Mar 6, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

Given the following code:

fn main() {
    let v: Vec<String> = Vec::new();
    let v2: Vec<String> = v.iter().filter(|s| s.len() == 2).collect();
}

The current output is:

error[E0277]: a value of type `Vec<String>` cannot be built from an iterator over elements of type `&String`
 --> src/main.rs:3:61
  |
3 |     let v2: Vec<String> = v.iter().filter(|s| s.len() == 2).collect();
  |                                                             ^^^^^^^ value of type `Vec<String>` cannot be built from `std::iter::Iterator<Item=&String>`
  |
  = help: the trait `FromIterator<&String>` is not implemented for `Vec<String>`

error: aborting due to previous error

Instead of somehow trying to call .clone() on the Strings, if v is not used anywhere else, one can easily fix this problem by calling v2 .. = v.into_iter() instead of v2 .. = v.iter().
Does it make sense for rustc to suggest that?

@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 6, 2021
@JohnTitor JohnTitor added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Mar 6, 2021
@MoreTacos
Copy link

I totally support this. A few months ago I tried to use some functional programming paradigms in Rust I got very confused with ownership when using iterators into maps.

If I would have known about into_iter my life would have been a lot easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants