Sub topic 16
Overriding some fields with ..Default::default()
This chapter assumes you know how to define structs and enums, and that you have seen #[derive(Default)] or an impl Default for ... block. Here you will combine a Default implementation with struct update syntax, so you set only the fields you care about.
The problem it solves
A struct with many fields is tedious to build when most values are ordinary. Writing every field by hand hides the two or three that matter. Struct update syntax lets you name those fields and fill in the rest from another value of the same type.
The syntax
Put ..expression at the end of a struct literal. Every field you did not list is taken from that expression. With Default, the expression is usually Default::default().
#[derive(Debug, Default)]
enum Mode {
#[default]
Normal,
Fast,
}
#[derive(Debug)]
struct Server {
host: String,
port: u16,
mode: Mode,
}
impl Default for Server {
fn default() -> Self {
Server {
host: "localhost".to_string(),
port: 8080,
mode: Mode::default(),
}
}
}
fn main() {
let s = Server {
port: 9000,
..Default::default()
};
println!("{:?}", s);
let fast = Server {
mode: Mode::Fast,
..Default::default()
};
println!("{:?}", fast);
}The first value prints Server { host: "localhost", port: 9000, mode: Normal }. The compiler infers that Default::default() means Server::default(), because the struct literal has type Server.
What to know before using it
..Default::default()must come last in the literal, with no trailing comma after it.- The default value is built in full first. Fields you override are then dropped unused. If a default is expensive, such as opening a file, that cost is still paid.
- Fields not listed are moved out of the default value. This is fine here because the default is a temporary. If you use
..other_valuewith a named variable, non-Copyfields such asStringare moved out of it, and you can no longer use that variable as a whole. - Private fields matter. Outside the module that defines the struct, you cannot use update syntax if the struct has private fields, because the syntax would need access to them.
Choosing derive or a manual impl
Derive Default when every field's own default is acceptable: 0, false, an empty String. Write it by hand when a sensible starting value differs, as with port: 8080 above. Either way, the ..Default::default() call site looks the same.
Exercise
Given #[derive(Debug, Default)] struct Settings { name: String, retries: u32, verbose: bool }, write an expression that creates a Settings with retries set to 3 and everything else at its default. What are the values of name and verbose?
Show answer
Settings { retries: 3, ..Default::default() }. The name field is an empty string "" and verbose is false.
Exercise
Why does Server { port: 9000, ..Default::default(), } with a trailing comma fail to compile?
Show answer
The .. base expression must be the last item in the struct literal, and a trailing comma after it is not allowed. Remove the comma after Default::default().