Stream read from nth byte to nth byte

So what I have is a stream and I want to read from the nth byte to the nth byte, this is my code rn and I want to know if there is any better way of doing this

f.read_to_end(&mut buffer).expect("buffer overflow");
let part_I_want = buffer[start..end];

I know I can read and make the buffer length to the end variable but then I am still reading all the start and I want to know if I can do this any better way.

You can require a Seek bound on the stream to efficiently skip over bytes you don't want to read. Otherwise you have to read them.

Then there's .take() wrapper that limits number of bytes read, and combined with read_to_end() it will read only up to the number of bytes.

1 Like

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.