I am trying to filter a Vec in place and keep getting errors and warnings.
In this example I'm trying to filter out the odd indexed items, and only retain the even indexes.
playground example
fn main() {
let mut list = vec!["some","thing","about","the","color","of","the","sky"];
list.iter_mut()
.enumerate()
.filter(|(index, _)| index % 2 == 0)
.map(|(_, item)| item.to_owned())
.collect::<Vec<String>>();
// this is another version that is similar, and also not working
// list.iter_mut()
// .enumerate()
// .filter_map(|(i, s)| if i % 2 == 0 { Some(s.to_owned()) } else { None })
// .collect::<Vec<String>>();
println!("{:?}", list);
}
but i get this warning about the return from collect
not being used, and the list
is not filtered.
warning: unused return value of `collect` that must be used
--> src/main.rs:3:5
|
3 | / list.iter_mut()
4 | | .enumerate()
5 | | .filter(|(index, _)| index % 2 == 0)
6 | | .map(|(_, item)| item.to_owned())
7 | | .collect::<Vec<String>>();
| |__________________________________^
|
= note: `#[warn(unused_must_use)]` on by default
= note: if you really need to exhaust the iterator, consider `.for_each(drop)` instead
any help would be appreciated!
edit: added original code