Given this definition for foo :
let foo = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
I would like to be able to write code as follows:
let result: Vec<_> = foo.iter() .enumerate() .flat_map(|(i, row)| if i % 2 == 0 { row.iter().map(|x| x * 2) } else { std::iter::empty() }) .collect();
but this causes an error regarding if and else clauses having incompatible types. I tried to temporarily remove map , and I tried to determine the empty vector outside the closure and return the iterator by so:
let empty = vec![]; let result: Vec<_> = foo.iter() .enumerate() .flat_map(|(i, row)| if i % 2 == 0 { row.iter() //.map(|x| x * 2) } else { empty.iter() }) .collect();
It seems silly, but it compiles. If I try to uncomment map , then it still complains about if and else clauses that have incompatible types. Here is the part of the error message:
error[E0308]: if and else have incompatible types --> src/main.rs:6:30 | 6 | .flat_map(|(i, row)| if i % 2 == 0 { | ______________________________^ 7 | | row.iter().map(|x| x * 2) 8 | | } else { 9 | | std::iter::empty() 10 | | }) | |_________^ expected struct `std::iter::Map`, found struct `std::iter::Empty` | = note: expected type `std::iter::Map<std::slice::Iter<'_, {integer}>, [ closure@src /main.rs:7:28: 7:37]>` found type `std::iter::Empty<_>`
Playground
I know that I can write something that does what I want with some nested for loops, but I would like to know if there is a way to write it using iterators.