title: How to iterate over the chars in a file
body: <p>Related: <a href="https://stackoverflow.com/questions/35385703/read-file-character-by-character-in-rust">Read file character-by-character in Rust</a>, but the answers there do not address the question of how to produce an <code>Iterator</code> that yields the characters in a file.</p>
  
  <p>I am writing a function with a signature like this:</p>
  
  <pre><code>fn lex(chars: Iterator&lt;Item=char&gt;) -&gt; Tokens {
  }
  </code></pre>
  
  <p>that takes in an iterator of <code>char</code>s, and returns another iterator.</p>
  
  <p>I want to be able to write tests for this function, e.g. by passing in the result of calling <code>chars()</code> on a string object.</p>
  
  <p>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.</p>
  
  <p>I would be happy to change the signature of the function, e.g. to take in an <code>Iterator&lt;Item=Result&lt;char, SomeErrorType&gt;&gt;</code>, but I do want to consume the file on demand, not read the whole thing in beforehand.</p>
  
  <p>I would be happy to use a <code>BufRead</code> and an <code>io::Cursor</code> in test if that helped.</p>
  
  <p>I looked at calling <code>chars()</code> on a <a href="https://github.com/rust-lang/rust/issues/27802" rel="nofollow noreferrer"><code>Read</code></a> but it is unstable and looks like it will never become stable.</p>
  
  <p>So, how do I iterate over the characters in a file?</p>