Fixed-size array with length specified at initialisation?

I would like to have a Struct that contains a byte (u8) array where the length of that array is specified as parameter to the new function. Can this be done? Maybe using some crate?

The length is actually a constant defined through toml_cfg::toml_config, but I didn't find any way to pass that to a type generic of my struct, only as parameter to the new function...

I'd prefer to have an uninitialized stack allocated array to that easy but potentially less performant Vector solution...

Generics must be known at compile time, while your config will only be know when your program is running, so there's no way to use it for an array size. Consider instead using a Vec.

1 Like

as far as I understood it my config.toml file is read at compile time and used to generate a static struct wit all my config options.

See also:

So my problem was that I've tried use generics to define my buffer's size, which need to be actual literals when instantiating them.

Getting rid of the generics part of my struct definition, importing the const/static? CONFIG struct like
use crate::CONFIG;
And defining the buffer in my struct like:
buffer: [u8; CONFIG.buffer_size],
solved my problem.

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.