Is it possible to avoid initializing buffers that you will overwrite with read?


#1

Is it possible to avoid initializing buffers that you will overwrite anyways with std::io::Read::read?

I know about std::mem::uninitialized but as far as I can see I cannot get variable sized memory blocks.


#2

You can create a vector with the appropriate capacity and set its length accordingly:

let mut foo = Vec::with_capacity(50);
unsafe {
    foo.set_len(50);
}
// etc.

#3

std::io::Read has https://doc.rust-lang.org/std/io/trait.Read.html#method.initializer. File, for example, returns a nop initializer - it will not zero any part of the buffer. So a Vec will be dynamically filled without any explicit initialization (zeroing)


#4

Thanks all. That’s exactly what I needed :slight_smile: