generic

Rust Generic structs

A generic struct describes a family of concrete record types. A label can contain a number or text while using the same field name. Each instance has one specific T. Expected output (not executed): 42 Desk Why: Label<u32> and Label<&str> are distinct types. Common mistake: Treating a generic field as a runtime slot that accepts unrelated types after initialization.

Formal Syntax & Structure

rust syntaxrust
struct Label<T> { value: T }

Working Example

Example Snippetrust
struct Label<T> { value: T }fn main() {    let id = Label { value: 42_u32 };    let name = Label { value: "Desk" };    println!("{} {}", id.value, name.value);}
Rust Reference