How is it possible to take an array with any size as a generic type in rust?

I would like to have a generic struct, that has an array of MyType in it. It can be any size but can not be changed, that is why I want to use generic.

I tried it like this:

pub struct MyStruct<[MyType, COUNT]> {
    data: [MyType, COUNT],

I get syntax error expected COMMA without any useful guides from the compiler.

>Solution :

The correct syntax is

pub struct MyStruct<MyType, const COUNT: usize> {
    data: [MyType; COUNT],

The parameter COUNT needs to be annotated as a constant. There are no square brackets around the type parameters. The delimiter between the type and the count in an array type is a semicolon, not a comma.

Leave a Reply