fnmain() {
letp = Point { x: 1.0, y: 2.0 };
println!(\"Point: ({}, {})\", p.x, p.y);
let person = Person {
name: String::from(\"Alice\"),
age: 30,
email: String::from(\"alice@example.com\"),
};
println!(\"Person: {}, age {}\", person.name, person.age);
}
Struct Field Init Shorthand
fncreate_person(name: String, age: u32) -> Person {
Person {
name, // Equivalent to name: name
age, // Equivalent to age: age
email: String::from(\"no-reply@example.com\"),
}
}
Struct Update Syntax
Create a new struct from an existing one:
letperson1 = Person {
name: String::from(\"Alice\"),
age: 30,
email: String::from(\"alice@example.com\"),
};
let person2 = Person {
name: String::from(\"Bob\"),
..person1 // Copy remaining fields from person1
};