Const generics

From Rust Community Wiki

Const generics are a nightly-only feature that is still in development. It will allow Rust code to be generic over values, such as numbers or strings.

A subset of const generics, called min_const_generics, has been stabilized and will land in Rust 1.51.


This example demonstrates how const generics can be used on nightly:


struct Array<T, const N: usize>([T; N]);

impl<T, const N: usize> Array<T, N> {
    fn new(value: T) -> Self
        T: Copy,
        Array([value; N])

let arr: Array<i32, 7> = Array::new(0);

See also[edit]