You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When deferencing an iterator of references to a Copy with .map(|a_ref| *a_ref) clippy suggests using .copied(), which works for immutable references but doesn't for mutables, as shown in the example below.
$ cat src/main.rs
fn main(){letmut three = 3;let three_ref = &mut three;let v:Vec<&muti32> = vec![three_ref];let _iter:Vec<i32> = v.into_iter().map(|i| *i).collect();}
$ cargo clippy
Checking clippy v0.1.0(/tmp/clippy)
warning:You are using an explicit closure for copying elements
--> src/main.rs:6:26
|
6 | let _iter:Vec<i32> = v.into_iter().map(|i| *i).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help:Consider calling the dedicated `copied` method: `v.into_iter().copied()`
|
= note: `#[warn(clippy::map_clone)]` on by default
= help:for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
$ cat src/main.rs
...
//let _iter: Vec<i32> = v.into_iter().map(|i| *i).collect();
let _iter:Vec<i32> = v.into_iter().copied().collect();
$ cargo clippy
Checking clippy v0.1.0(/tmp/clippy)
error[E0271]:type mismatch resolving `<std::vec::IntoIter<&muti32>as std::iter::Iterator>::Item == &_`
--> src/main.rs:7:41
|
7 | let _iter:Vec<i32> = v.into_iter().copied().collect();
| ^^^^^^ types differ in mutability
|
= note: expected type `&mut i32`
found reference `&_`
error[E0599]: no method named `collect` found for struct `std::iter::Copied<std::vec::IntoIter<&muti32>>` in the current scope
--> src/main.rs:7:50
|
7 | let _iter:Vec<i32> = v.into_iter().copied().collect();
| ^^^^^^^ method not found in `std::iter::Copied<std::vec::IntoIter<&muti32>>`
|
= note: the method `collect` exists but the following trait bounds were not satisfied:
`<std::vec::IntoIter<&muti32>as std::iter::Iterator>::Item = &_`
which is required by `std::iter::Copied<std::vec::IntoIter<&muti32>>: std::iter::Iterator`
`std::iter::Copied<std::vec::IntoIter<&muti32>>: std::iter::Iterator`
which is required by `&mut std::iter::Copied<std::vec::IntoIter<&muti32>>: std::iter::Iterator`
When deferencing an iterator of references to a Copy with
.map(|a_ref| *a_ref)
clippy suggests using.copied()
, which works for immutable references but doesn't for mutables, as shown in the example below.Clippy version:
The text was updated successfully, but these errors were encountered: