Similar to map but if allows to drop an item.
1
2
3
4
5
6
7
8
let text_nums = ["1", "two", "three", "4"];
let nums: Vec<_> = text_nums
.iter()
.filter_map(|x| x.parse::<u32>().ok())
.collect();
assert_eq!(nums, vec![1, 4]);
Similar to map but if allows to drop an item.
1
2
3
4
5
6
7
8
let text_nums = ["1", "two", "three", "4"];
let nums: Vec<_> = text_nums
.iter()
.filter_map(|x| x.parse::<u32>().ok())
.collect();
assert_eq!(nums, vec![1, 4]);
A new version of content is available.