Field init shorthand
In older Rust, when initializing a struct, you must always give the full set of key: value
pairs
for its fields:
# #![allow(unused_variables)] #fn main() { struct Point { x: i32, y: i32, } let a = 5; let b = 6; let p = Point { x: a, y: b, }; #}
However, often these variables would have the same names as the fields. So you'd end up with code that looks like this:
let p = Point {
x: x,
y: y,
};
Now, if the variable is of the same name, you don't have to write out both, just write out the key:
# #![allow(unused_variables)] #fn main() { struct Point { x: i32, y: i32, } let x = 5; let y = 6; // new let p = Point { x, y, }; #}