pattern

Rust Destructure named struct fields

A struct pattern can select named fields, rename their bindings and ignore remaining fields with `..`. Expected output (not executed): 12 Why: The field weight is read into a new binding called kg. Common mistake: A colon inside this pattern renames a field binding; it is not a type annotation.

Formal Syntax & Structure

rust syntaxrust
let Type { field: binding, .. } = value;

Working Example

Example Snippetrust
struct Parcel { weight: u32, fragile: bool }
fn main() {    let parcel = Parcel { weight: 12, fragile: true };    let Parcel { weight: kg, .. } = parcel;    println!("{kg}");}
Rust Reference