rust / intermediate
Snippet
Struct Update Syntax with ..
The struct update syntax allows creating a new struct instance while copying most fields from an existing one. Fields not explicitly set take their values from the source struct, reducing boilerplate when working with similar configurations.
snippet.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#[derive(Default, Debug)]struct Config {timeout: u32,max_retries: u32,debug_mode: bool,}fn main() {let default_cfg = Config::default();let custom_cfg = Config {timeout: 30,..default_cfg};println!("{:?}", custom_cfg);}
Breakdown
1
..default_cfg
Copies all remaining fields from default_cfg that aren't explicitly set
2
timeout: 30,
Only timeout is changed; other fields come from default_cfg