I would like to know if it is possible to get all the indexes that fulfill a condition in a rust vector datatype. I know the trait operator provides a method to find the first element that does it:
let test=vec![1,0,0,1,1];
let index = test.iter().position(|&r| r == 1);
println!("{}",index) //0
However, I would be interested in obtaining all the indexes that are equal to 1 in the test vec.
let test=vec![1,0,0,1,1];
let indexes = //Some Code
println!("{:?}",indexes) //0,3,4
What should I do?
then_some
:test.iter().enumerate().filter_map(|(index, &r)| (r == 1).then_some(index)).collect::<Vec<_>>()
– Quite