How to implement a array with several structs which contains arrays with different lengths

pub struct Prime {
    pub length: u32,
    pub data: [u8; 96],
}

pub struct CryptoDhConstParaS {
    pub p: Prime,
    pub generator: u8,
    pub x_bits: u32,
}

pub const G_DH_PARA: [CryptoDhConstParaS; 1] = [
    CryptoDhConstParaS{
        p: Prime {
            length: 96,
            data: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0xC2,
                0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0x1C,
                0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0xCC,
                0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x9B,
                0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0x04,
                0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x43,
                0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x14,
                0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0xC2,
                0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0x7E,
                0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x3A, 0x36, 0x36,
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
        },
        generator: 2,
        x_bits: 768,
    }];

If I only put one struct in the array, I give the data in Prime a certain length.and It works well.
But what I want is

pub const G_DH_PARA: [CryptoDhConstParaS; 6] = [
    CryptoDhConstParaS{
        p: Prime {
            length: 96,
            data: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0xC2,
                0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0x1C,
                0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0xCC,
                0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x9B,
                0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0x04,
                0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x43,
                0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x14,
                0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0xC2,
                0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0x7E,
                0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x3A, 0x36, 0x36,
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
        },
        generator: 2,
        x_bits: 768,
    },
CryptoDhConstParaS{
        p: Prime {
            length: 128,
            data: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0xC2,
                0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0x1C,
                0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0xCC,
                0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x9B,
                0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0x04,
                0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x43,
                0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x14,
                0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0xC2,
                0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0x7E,
                0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x3A, 0x36, 0x36,
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
        },
        generator: 2,
        x_bits: 1024,
    }
......

];

The data in each Prime has different lenght.

Of course, I tried to use a Vec like this

pub struct Prime {
    pub length: u32,
    pub data: Vec<u8>,
}

pub struct CryptoDhConstParaS {
    pub p: Prime,
    pub generator: u8,
    pub x_bits: u32,
}

But I encoutered an error like this "error[E0010]: allocations are not allowed in constants"

If you are only using them for globals, then you can use slices

pub struct Prime {
    pub length: u32,
    pub data: &'static [u8],
}

pub struct CryptoDhConstParaS {
    pub p: Prime,
    pub generator: u8,
    pub x_bits: u32,
}

pub const G_DH_PARA: [CryptoDhConstParaS; 6] = [
    CryptoDhConstParaS{
        p: Prime {
            length: 96,
            data: &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0xC2,
                0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0x1C,
                0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0xCC,
                0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x9B,
                0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0x04,
                0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x43,
                0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x14,
                0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0xC2,
                0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0x7E,
                0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x3A, 0x36, 0x36,
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
        },
        generator: 2,
        x_bits: 768,
    },
    ...]
1 Like

Thanks a lot! It works well!!