generic

Rust Default type parameters

A default type argument makes a common instantiation shorter to write in type positions. It does not force all values constructed without annotation to use that default. Expected output (not executed): 7 A Why: The annotation Record uses the default u32; the second annotation overrides it. Common mistake: Expecting a default parameter to override inference from explicit constructor inputs.

Formal Syntax & Structure

rust syntaxrust
struct Record<T = u32>(T);

Working Example

Example Snippetrust
struct Record<T = u32>(T);fn main() {    let id: Record = Record(7);    let text: Record<&str> = Record("A");    println!("{} {}", id.0, text.0);}
Rust Reference