How to `read_until` with maximum read limit

Hi

I'd like to read a file with separator '>' with maximum read limit.

fn main() {
  let mut f = BufReader::new(File::open("Bigfile.txt").unwrap());
  let mut v = Vec::new();

  for _i in 0..100 {
    let tf = f.take(2000);
    tf.read_until(b'>', &mut v).unwrap();
  
    f = tf.into_inner();
  }
  
}

Is this okay?
I'm not sure but I think that take() and into_inner() take some time.

BufReader::take is actually Read::take, which literally only creates the struct. Likewise, Take::into_inner moves the field out of struct. The only overhead could be in the implementation of BufRead for Take, but it mostly delegates the work to the inner reader and so should be not worse than anything you could do yourself.

2 Likes

"Thinking" is a spectacularly bad way to understand performance, btw. The only valid way is to measure.

2 Likes

You can avoid the into_inner dance if you do f.by_ref().take(2000) (or equivalently (&mut f).take(2000)).

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.