Const generics let a type or struct parameterize a compile-time constant, such as an array’s length. They avoid creating a separate struct for every size and can keep fixed-size data in an array rather than a runtime-resizable Vec. section 7.1
#[derive(Debug)]
struct Buffer<T, const LENGTH: usize> {
buf: [T; LENGTH],
}
The element type and constant length are separate parameters; LENGTH sets the array’s size. section 7.1
Use an integer-based primitive such as usize for the constant parameter; char and bool are also allowed, but floating-point values are not. A generic conversion implementation can accept arrays of different lengths while preserving each length in the Buffer type. section 7.1
impl<T, const LENGTH: usize> From<[T; LENGTH]> for Buffer<T, LENGTH> {
fn from(buf: [T; LENGTH]) -> Self {
Buffer { buf }
}
}
This implementation converts an array into a Buffer with the same element type and length. section 7.1
Infer a buffer from an array
Converting [0, 1, 2, 3] with Buffer::from produces a Buffer containing those four values. section 7.1

