That's not what map does. If you want it to stop after something, you could consider using take_while.
(Note that if it was going to stop on None, then it shouldn't ever output Some(10), but just 10, because giving out options-that-aren't-supposed-to-be-none is just a nuisance for the caller.)
First, we call into_iter() on the value. Then, we match on the iterator that returns, calling next over and over until we see a None . At that point, we break out of the loop, and we’re done iterating.
I think for in loop will break if it encounter first None, Does it?
So, the return value of map closure is B, not Option<B>.
As the code of mine above, map closure treats the return value as Some(None) and Some(Some(v)).
Using map will really just modify each element, and if you make the closure return Some or None, then the new iterator will return Some(Some(_)) or Some(None) until it finally returns None. So you're adding an extra Option layer.
Consider this:
fn main() {
let x1 = [10, 11].into_iter();
for t in x1 {
println!("{t:?}");
}
let x1 = [10, 11].into_iter();
let x2 = x1.map(|v| Some(v));
for t in x2 {
println!("{t:?}");
}
let x1 = [10, 11].into_iter();
let x2 = x1.map(|v| Some(v));
let x3 = x2.map(|v| Some(v));
for t in x3 {
println!("{t:?}");
}
}
struct MyIter<'a> {
data: &'a [i32],
index: usize,
}
impl<'a> Iterator for MyIter<'a> {
type Item = i32;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.data.len() {
return None;
}
let value: i32 = self.data[self.index];
if value == 13 {
// if we increment `self.index` here, the iterator will not be fused, but it still works in this example:
//self.index += 1;
None
} else {
self.index += 1;
Some(value)
}
}
}
fn main() {
let x1 = MyIter { data: &[10, 11, 12, 13, 14, 15, 16], index: 0 };
for v in x1 {
println!("{:?}", v);
}
}
Here, None really means that the iteration is finished. This works even if you comment-in self.index += 1 in the example above. But then the iterator isn't fused.
Edit: I forgot to check self.index >= self.data.len() in the example above and just corrected it.
A good mental exercise is to consider what would happen if you didn't explicitly return an Option, ie. no Some or None, just a completely different value. How should the iterator know when to stop in such a case?
None is not special like NULL or nil. It doesn't "flatten" automatically, so map preserves what you return without confusing None-from-map with None-from-iterator.
To stop iteration on None, you can use from_fn instead of mapping an existing iterator.