Read specific number of bytes from stream into buffer

I need to read a specific number of bytes from a stream (socket) into a buffer. The number of bytes is known at runtime, but not at compile time.

This works, but is there any better/simpler ways of doing it:

        let size: usize = get_size(); // not known at compile time
        let mut buf = Vec::with_capacity(size);
        buf.resize(size, 0);
        stream.read_exact(&mut buf)?;
let mut buf = vec![0; size];

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.