What is it that you actually want to do? The following compiles:
let mut iter = a
.into_iter()
.map(|x| { Some((*x)?) })
.for_each(|x| println!("{:?}", x));
but it isn't very useful. The above turns the &Option<i32> into an Option<i32> via *x, returns None if that results in None, and otherwise wraps the value in Some, so it's essentially .map(|x| *x).
I believe you are looking for the filter_map iterator adapter. This method will filter out all elements that return None and keep those that return Some(_) and returns the item inside the Some(_).
It can be used like this:
fn t() -> Option<()> {
let a = [Some(0), None, Some(2)];
let iter = a.into_iter()
.filter_map(|x| *x)
.for_each(|x| println!("{}", x));
Some(())
}
fn main() {
println!("{}", t().is_none())
}
I was going to suggest filter_map as well until I saw @yaa110 mentioning that they want to return from t() when a None is seen - that's different semantic from filter_map.
For cases where you return Result, it's useful to know that .collect() can take iterator of Result<T,E> and collect into Result<Vec<T>, E>, so you can then use ? on the collection: