How would you iterate through a list and filter out all values where the response Result is not Ok? I want to do something like filter_map, but it is saying I need to return an Option.
use std::result::Result;
fn match_value(vals: i32) -> Result<i32, i32> {
match vals {
2 => Ok(1),
_ => Err(0),
}
}
fn main() {
let values = vec![1, 2, 3, 2];
let matching = values
.iter()
.map(|name| match_value(*name))
.filter(|x| x.is_ok())
.collect::<Vec<_>>();
println!("{:?}", matching);
}
If you want the value contained in the Ok, use Iterator::flat_map, which combines iterable values into one iterator. Option and Result both implement IntoIterator:
let matching = values
.iter()
.flat_map(|name| match_value(*name))
.collect::<Vec<_>>();
If you want the original value, just use filter:
let matching = values
.iter()
.filter(|&&name| match_value(name).is_ok())
.collect::<Vec<_>>();
There's no need to import Result; it's already part of the prelude.
Not sure if this is still relevant, but maybe you wanted sth like this
let matching = values
.iter()
.filter_map(|name| match_value(*name).ok())
.collect::<Vec<_>>();
In case of an error ok() will return None which is then filtered out.
Ok(T) values are converted into the origial T. So in the end, you will have an Vec<T>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With