Back to questions
JuniorTheoryOccasional
What does the program print?
struct Point {
x: i32,
y: i32,
}
fn main() {
let p1 = Point { x: 1, y: 2 };
let p2 = Point { x: 5, ..p1 };
println!("{} {}", p2.x, p2.y);
}5 2
- Expecting
1 2:..p1takes only the fields the literal does not list, so the explicitx: 5stays and onlyycomes fromp1. - Expecting
5 0or an error for the missingy:..p1fills every unlisted field fromp1, and a field never gets a default value on its own.
- What happens to
p1if the fields taken through..p1areStrings? - Where in the literal must
..p1stand, and can a comma follow it?