Parsing iterator over a file


#1

Hello everyone! A Python guy trying out Rust here for the first time here :-).

I want to create an iterator over a file that would read the file contents in chunks of pre-defined size and yield strings to a consumer. The question is how do I go from a straightforward loop:

let mut buf = [0; BUFSIZE];
loop {
    let size = match f.read(&mut buf) {
        Err(error) => panic!("...", error),
        Ok(0) => break,
        Ok(result) => result,
    };
    print!("{}", str::from_utf8(&buf[0..size]).unwrap());
}

to an implementation of an Iterator<String> trait so it could be used like this:

for chunk in chunks(f) {
  print!("{}", chunk);
}

I tried to look at core::iter::iterate but can’t really wrap my head around the correct syntax.

Thanks!


#2

According to discussion on IRC, this is superseded by File reading iterator. (Was deleted by the spam filter, now unstuck.)