Related: Read file character-by-character in Rust, but the answers there do not address the question of how to produce an Iterator
that yields the characters in a file.
I am writing a function with a signature like this:
fn lex(chars: Iterator<Item=char>) -> Tokens {
}
that takes in an iterator of char
s, and returns another iterator.
I want to be able to write tests for this function, e.g. by passing in the result of calling chars()
on a string object.
In my product, on the code, I want to read a text file and pass the characters in that file in to my function. For the moment, I don't care what happens if the file is not valid UTF-8.
I would be happy to change the signature of the function, e.g. to take in an Iterator<Item=Result<char, SomeErrorType>>
, but I do want to consume the file on demand, not read the whole thing in beforehand.
I would be happy to use a BufRead
and an io::Cursor
in test if that helped.
I looked at calling chars()
on a Read
but it is unstable and looks like it will never become stable.
So, how do I iterate over the characters in a file?