Hi everyone,
I know similar issues exist (see this one), but I cannot find a way to solve mine.
My problem is that I cannot find a common object that would make this compile. You have a MWE just below. In general, I want to specify that my return value is always going to be some iterator of a known (and fixed) item type.
Thanks in advance !
fn main() {
let a = true;
let x = vec![1, 2, 3, 4].into_iter();
let x = x.filter(|e| *e > 1);
let x = if a {
x.filter(|e| true) // as ?
} else {
x // as ?
};
for e in x {
println!("{}", e);
}
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:10:9
|
5 | let x = x.filter(|e| *e > 1);
| ---------- the found closure
6 |
7 | let x = if a {
| _____________-
8 | | x.filter(|e| true) // as ?
| | ------------------ expected because of this
9 | | } else {
10 | | x // a ?
| | ^ expected struct `Filter`, found struct `std::vec::IntoIter`
11 | | };
| |_____- `if` and `else` have incompatible types
|
= note: expected type `Filter<Filter<std::vec::IntoIter<_>, [closure@src/main.rs:5:22: 5:32]>, [closure@src/main.rs:8:18: 8:26]>`
found struct `Filter<std::vec::IntoIter<_>, [closure@src/main.rs:5:22: 5:32]>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error