Skip to content

Commit 531c3c2

Browse files
committed
Reduce rectangle ambiguity
changed field names to `top_left` and `bottom_right`
1 parent e60b98d commit 531c3c2

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/custom_types/structs.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ struct Point {
2929
// Structs can be reused as fields of another struct
3030
#[allow(dead_code)]
3131
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,
3436
}
3537
3638
fn main() {
@@ -44,23 +46,26 @@ fn main() {
4446
4547
4648
// 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 };
4850
4951
// Access the fields of the point
5052
println!("point coordinates: ({}, {})", point.x, point.y);
5153
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);
5661
5762
// 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;
5964
6065
let _rectangle = Rectangle {
6166
// 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,
6469
};
6570
6671
// Instantiate a unit struct

src/std/box.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ struct Point {
1818
y: f64,
1919
}
2020
21+
// A Rectangle can be specified by where its top left and bottom right
22+
// corners are in space
2123
#[allow(dead_code)]
2224
struct Rectangle {
23-
p1: Point,
24-
p2: Point,
25+
top_left: Point,
26+
bottom_right: Point,
2527
}
2628
2729
fn origin() -> Point {
@@ -38,14 +40,14 @@ fn main() {
3840
// Stack allocated variables
3941
let point: Point = origin();
4042
let rectangle: Rectangle = Rectangle {
41-
p1: origin(),
42-
p2: Point { x: 3.0, y: 4.0 }
43+
top_left: origin(),
44+
bottom_right: Point { x: 3.0, y: -4.0 }
4345
};
4446
4547
// Heap allocated rectangle
4648
let boxed_rectangle: Box<Rectangle> = Box::new(Rectangle {
47-
p1: origin(),
48-
p2: origin()
49+
top_left: origin(),
50+
bottom_right: Point { x: 3.0, y: -4.0 },
4951
});
5052
5153
// The output of functions can be boxed

0 commit comments

Comments
 (0)