Create a buffer of fixed size not possible due not a constant var

Hi,

Is there a way to make a fixed sized buffer from a variable. Let me explain myself.

let filesize = 1024;
let mut buf = [0u8; filesize]; // wont work

i reading a file from the filesystem and want to make a buffer that fit its size, so i can read from it and return the bytes somewhere else.

Some suggestions?

Greeting A.

1 Like

"fixed sized" and "variable" are fundamentally incompatible.

You can have either fixed size...

const FILESIZE: usize = 1024;
let mut buf = [0u8; FILESIZE];

...or variable.

let filesize = 1024;
let mut buf = vec![0u8; filesize];
3 Likes

So i can assume here that a Vec is dynamic growable? A bit like slice in Go?
thanks for the fast reply.

Yes; see the book chapter on Vectors.

1 Like

so the most idiomatic way to buffer giant data like 4gb videos, is just read the file to a Vec with some kind of already bootstrapped capacity?

So reading giant data like a 4gb video is just to open the file and read it to a Vec ?