@@ -29,8 +29,10 @@ struct Point {
29
29
// Structs can be reused as fields of another struct
30
30
#[allow(dead_code)]
31
31
struct Rectangle {
32
- p1: Point,
33
- p2: Point,
32
+ // A rectangle can be specified by where the top left and bottom right
33
+ // corners are in space.
34
+ top_left: Point,
35
+ bottom_right: Point,
34
36
}
35
37
36
38
fn main() {
@@ -44,23 +46,26 @@ fn main() {
44
46
45
47
46
48
// Instantiate a `Point`
47
- let point: Point = Point { x: 0 .3, y: 0.4 };
49
+ let point: Point = Point { x: 10 .3, y: 0.4 };
48
50
49
51
// Access the fields of the point
50
52
println!("point coordinates: ({}, {})", point.x, point.y);
51
53
52
- // Make a new point by using struct update syntax to use the fields of our other one
53
- let new_point = Point { x: 0.1, ..point };
54
- // `new_point.y` will be the same as `point.y` because we used that field from `point`
55
- println!("second point: ({}, {})", new_point.x, new_point.y);
54
+ // Make a new point by using struct update syntax to use the fields of our
55
+ // other one
56
+ let bottom_right = Point { x: 5.2, ..point };
57
+
58
+ // `bottom_right.y` will be the same as `point.y` because we used that field
59
+ // from `point`
60
+ println!("second point: ({}, {})", bottom_right.x, bottom_right.y);
56
61
57
62
// Destructure the point using a `let` binding
58
- let Point { x: my_x , y: my_y } = point;
63
+ let Point { x: top_edge , y: left_edge } = point;
59
64
60
65
let _rectangle = Rectangle {
61
66
// struct instantiation is an expression too
62
- p1 : Point { x: my_y , y: my_x },
63
- p2: point ,
67
+ top_left : Point { x: left_edge , y: top_edge },
68
+ bottom_right: bottom_right ,
64
69
};
65
70
66
71
// Instantiate a unit struct
0 commit comments