At the moment I have a function with a signature: pub fn lex(r: &mut io::Read) -> Vec<Lexeme>
, in this function I use Vec::from_iter(an_iterator.filter_map(|x| x))
and I would like to get rid of the vector-generating step, so that I can work lazily on this list. From Returning Iterators I figured that I should write something like this:
pub fn lex<I: Iterator<Item = Lexeme>>(r: &mut io::Read) -> I
Unfortunately this doesn't work, and I don't know how to make it work
error[E0308]: mismatched types
--> src/lib/lib.rs:42:5
|
42 | r.chars()
| ^ expected type parameter, found struct `std::iter::FilterMap`
|
= note: expected type `I`
found type `std::iter::FilterMap<std::iter::Map<std::io::Chars<&mut std:
:io::Read>, [closure@src/lib/lib.rs:43:14: 67:10 lineno:_, col:_]>, [closure@src/lib/l
ib.rs:68:21: 68:26]>`
EDIT: Having thought about it a little longer, I guess I have to implement my own iterator, not only for type-system reasons, but also for tracking the state of the iteration.