I read somewhere that an Option is an iterator over either one or zero values, depending on whether it's a None or a Some. So it occurred to me to use flat_map() to filter out Nones.
for (line_num, line) in reader
.lines()
.flat_map(|lineopt| lineopt)
.enumerate()
{
if line.contains(&args.pattern) {
println!("{}: {}", line_num, line);
}
}
It works but seems kind of weird. Is the done thing, or is there a better way?
Semantically, this is also the same as .filter_map(|lineopt| lineopt) or filter_map(std::convert::identity), which can probably show the intent more clearly.