generic

Rust Const generic arguments

A complex concrete constant argument can be written inside braces. This does not imply arbitrary arithmetic on a generic const parameter is supported on the pinned stable baseline. Expected output (not executed): 5 Why: The constant expression evaluates to 5; N is used directly as the array length. Common mistake: Assuming a generic length expression like N + 1 is generally accepted in stable array types.

Formal Syntax & Structure

rust syntaxrust
make::<{ constant_expression }>()

Working Example

Example Snippetrust
fn slots<const N: usize>() -> [u8; N] { [0; N] }fn main() {    let data = slots::<{ 2 + 3 }>();    println!("{}", data.len());}
Rust Reference