Sub topic 4
Building from another instance with struct update syntax
This chapter assumes you can define a struct, create an instance with field init shorthand, and read the ownership rules for moves and borrows. It covers struct update syntax, which fills in the fields you don't list.
The basic form
Struct update syntax lets you write only the fields that differ and take the rest from another value. You put .. followed by an expression at the end of the field list.
#[derive(Debug, Clone, Default)]
struct Config {
name: String,
retries: u32,
verbose: bool,
}
fn main() {
let base = Config {
name: String::from("base"),
..Default::default()
};
let custom = Config {
retries: 5,
..base.clone()
};
println!("{:?}", base);
println!("{:?}", custom);
}The expression after .. must produce a value of the same struct type. Here Default::default() supplies retries and verbose, and base.clone() supplies name and verbose for custom. The ..expr part must come last, and it has no trailing comma.
Moves and partial moves
Struct update syntax moves or copies each field it takes, according to that field's type. Fields that implement Copy, such as u32 and bool, are copied. Fields such as String are moved.
This matters when you write ..base without .clone(). The following code fails to compile.
#[derive(Debug, Default)]
struct Config {
name: String,
retries: u32,
verbose: bool,
}
fn main() {
let base = Config {
name: String::from("base"),
..Default::default()
};
let custom = Config {
retries: 5,
..base
};
println!("{:?}", custom);
println!("{:?}", base); // error: borrow of partially moved value
}The name field moved out of base into custom. After that, base is partially moved, so you can't use it as a whole. You can still read the fields that were copied, such as base.retries, because copying leaves the original intact.
The rule is about which fields are actually taken. If you list every non-Copy field yourself, nothing is moved from the source, and base stays fully usable.
#[derive(Debug, Default)]
struct Config {
name: String,
retries: u32,
verbose: bool,
}
fn main() {
let base = Config::default();
let custom = Config {
name: String::from("custom"),
..base
};
println!("{:?} {:?}", custom, base);
}Only retries and verbose come from base, and both are Copy, so base is still valid.
Common errors
- Writing
..baseand then usingbaseas a whole. Clone the source with..base.clone(), or list the non-Copyfields yourself. - Putting
..expranywhere except the end of the field list. - Passing a value of a different struct type. The source must have exactly the same type, including generic parameters.
- Expecting private fields to be filled in from another module. Update syntax needs every field it fills to be visible at the call site, so it fails for structs with private fields outside their module.
Exercise
Given struct User { name: String, age: u32 } and let a = User { name: String::from("Ann"), age: 30 };, you write let b = User { age: 31, ..a };. Can you still use a.name afterwards? Can you use a.age?
Show answer
You can't use a.name, because the String was moved into b. You can use a.age, because age was listed explicitly in b and a's u32 is Copy anyway.