I have something that is Read
; currently it's a File
. I want to read a number of bytes from it that is only known at runtime (length prefix in a binary data structure).
So I tried this:
let mut vec = Vec::with_capacity(length);
let count = file.read(vec.as_mut_slice()).unwrap();
but count
is zero because vec.as_mut_slice().len()
is zero as well.
[0u8;length]
of course doesn't work because the size must be known at compile time.
I wanted to do
let mut vec = Vec::with_capacity(length);
let count = file.take(length).read_to_end(vec).unwrap();
but take
's receiver parameter is a T
and I only have &mut T
(and I'm not really sure why it's needed anyway).
I guess I can replace File
with BufReader
and dance around with fill_buf
and consume
which sounds complicated enough but I still wonder: Have I overlooked something?