keyword

Rust struct: group related fields into a type

A struct defines a named type that groups related fields. It can represent a real-world record, such as a product and its quantity. Expected output (not executed): Pen: 3 Why: The struct stores both values together and fields are accessed by name. Common mistake: Struct field types must be declared; Rust does not infer field types from each instance.

Formal Syntax & Structure

rust syntaxrust
struct Name { field: Type }

Working Example

Example Snippetrust
struct Product { name: String, quantity: u32 }
fn main() {    let product = Product { name: String::from("Pen"), quantity: 3 };    println!("{}: {}", product.name, product.quantity);}
Rust Reference