especially for iterating over strings, rust has a lines function for buffers ( http://doc.rust-lang.org/std/io/trait.BufferPrelude.html#tymethod.lines ).
In your case, you will iterate over the lines, and after reaching the EOF, the cycle is terminated automatically without your intervention.
for line in file.lines() { match line { Ok(line) => { // do stuff }, Err(_) => { println!("Unexpected error reading file.") } } }
or, if your function returns a compatible Result , you can use try! macro for less noise:
fn myfun(file: File) -> IoResult<()> { for line in file.lines() { let line = try!(line);
source share