Storing structs in an array with different generics

I have a struct with a generic:

struct SomeStruct<const SOME_ARRAY_SIZE: usize> {
    some_array: [i32; SOME_ARRAY_SIZE],
}

And I want to store an array of the struct in an array like this:

fn main() {
    let some_array_of_some_struct: [/* What should I put here? */; 3] = [ 
        SomeStruct<1> {
            some_array: [8],
        }, 
        SomeStruct<2> {
            some_array: [2, 6],
        }, 
        SomeStruct<3> {
            some_array: [4, 5, 12],
        }, 
    ];
}

How would I do this?

Arrays elements must all have the same type, including parameters, so you can't (without type erasure anyway). Lacking any further information, I'd say use a tuple instead.

3 Likes

Would a tuple have a runtime speed downside?

No.

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.