#209 Jun 18, 2026

209. ..Default::default() — set the fields you care about, default the rest

Building a struct with a dozen fields when you only want to change one is tedious. Struct update syntax lets you fill in the rest from a default — or from any other value.

Say you have a config struct. Spelling out every field just to flip one flag is noise:

1
2
3
4
5
6
7
let cfg = ServerConfig {
    host: String::new(),
    port: 8080,
    max_connections: 0,
    use_tls: true,
    timeout_secs: 0,
};

Derive Default, then use ..Default::default() to fill the fields you didn’t mention:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#[derive(Debug, Default, PartialEq)]
struct ServerConfig {
    host: String,
    port: u16,
    max_connections: u32,
    use_tls: bool,
    timeout_secs: u64,
}

let cfg = ServerConfig {
    port: 8080,
    use_tls: true,
    ..Default::default()
};

assert_eq!(cfg.port, 8080);
assert_eq!(cfg.use_tls, true);
assert_eq!(cfg.host, "");        // String::default()
assert_eq!(cfg.max_connections, 0);

The ..base part isn’t limited to Default — you can spread from any existing instance, so it doubles as a cheap “clone but tweak”:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
let base = ServerConfig {
    max_connections: 1024,
    ..Default::default()
};

let derived = ServerConfig {
    port: 9090,
    ..base
};

assert_eq!(derived.port, 9090);
assert_eq!(derived.max_connections, 1024);

Two things to remember: the ..rest has to come last, and the fields it pulls in are moved out of the source value (or copied, if they’re Copy) — so a non-Copy field like host means you can’t keep using base afterward.

← Previous 208. f64::mul_add — One Rounding, One Instruction, Better Accuracy