I want to get the elements in an array where a condition is true. For example. I would like all indices where the array elements are 0:
fn main() {
let lim = 10;
let mut sieve = vec![0; lim + 1];
sieve[1] = 1;
println!(
"{:?}",
sieve
.iter()
.enumerate()
.filter(|&(_, c)| c != 0)
.map(|(i, _)| i)
.collect::<Vec<usize>>()
);
}
But this is a compile error:
error[E0277]: can't compare `&{integer}` with `{integer}`
--> src/main.rs:10:33
|
10 | .filter(|&(_, c)| c != 0)
| ^^ no implementation for `&{integer} == {integer}`
|
= help: the trait `std::cmp::PartialEq<{integer}>` is not implemented for `&{integer}`
When I use c.clone() != 0
it works.
If I understand the error message correctly, Rust complains that it can't compare a borrow to an integer with an integer. I don't see why it shouldn't be possible.