expression

Rust Struct update syntax

Create a new struct using specified fields and remaining fields from a base value of the same type. Omitted fields must be accessible; non-Copy fields can be moved. Expected output (not executed): 2 false Why: Volume is replaced, and muted is copied from base. Common mistake: Do not assume struct update clones an owned String field; it may move that field.

Formal Syntax & Structure

rust syntaxrust
Type { field: value, ..base }

Working Example

Example Snippetrust
#[derive(Debug)]struct Settings { volume: u8, muted: bool }
fn main() {    let base = Settings { volume: 5, muted: false };    let quiet = Settings { volume: 2, ..base };    println!("{} {}", quiet.volume, quiet.muted);}
Rust Reference