generic

Rust Default const parameters

A const default supplies a common value when the argument is omitted in an appropriate type position. Callers can still request another fixed size. Expected output (not executed): 4 2 Why: The annotations select the default size 4 and explicit size 2. Common mistake: Assuming a default lets an instance change its length at runtime.

Formal Syntax & Structure

rust syntaxrust
struct Window<const N: usize = 4>([u8; N]);

Working Example

Example Snippetrust
struct Window<const N: usize = 4>([u8; N]);fn main() {    let a: Window = Window([0; 4]);    let b: Window<2> = Window([0; 2]);    println!("{} {}", a.0.len(), b.0.len());}
Rust Reference