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

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.

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.
1 Like

std::io::Read has Read in std::io - Rust. 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 Likes

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