Rust, typed: const generics

What are const generics?

const generics, also known as generic constants, are constant values that are parameterized into the type of a value. For example, a u64 array of length 10 has a type of [u64; 10]. The length, 10, is part of the type signature.

For a long time, arrays had a special place in the type system for their length. This quirky feature of arrays has since been extended to any type definition that could benefit from constant parameters.

How do you define const generics?

To define a generic constant, the constant parameters are placed at the end of the parameterized types with a special const keyword to mark them as const generics. The most immediate application of this new feature is to enable more meaningful interaction with arrays of generic size.

struct Array2<T, const WIDTH: usize, const HEIGHT: usize> {
    array: [[T; WIDTH]; HEIGHT],
}