@@ -9,7 +9,8 @@ let origin_x = 0;
99let origin_y = 0 ;
1010```
1111
12- A ` struct ` lets us combine these two into a single, unified datatype:
12+ A ` struct ` lets us combine these two into a single, unified datatype with ` x `
13+ and ` y ` as field labels:
1314
1415``` rust
1516struct Point {
@@ -32,7 +33,7 @@ We can create an instance of our `struct` via `let`, as usual, but we use a `key
3233value` style syntax to set each field. The order doesn’t need to be the same as
3334in the original declaration.
3435
35- Finally, because fields have names, we can access the field through dot
36+ Finally, because fields have names, we can access them through dot
3637notation: ` origin.x ` .
3738
3839The values in ` struct ` s are immutable by default, like other bindings in Rust.
@@ -67,9 +68,8 @@ struct Point {
6768
6869Mutability is a property of the binding, not of the structure itself. If you’re
6970used to field-level mutability, this may seem strange at first, but it
70- significantly simplifies things. It even lets you make things mutable for a short
71- time only:
72-
71+ significantly simplifies things. It even lets you make things mutable on a temporary
72+ basis:
7373
7474``` rust,ignore
7575struct Point {
@@ -82,7 +82,7 @@ fn main() {
8282
8383 point.x = 5;
8484
85- let point = point; // this new binding can’t change now
85+ let point = point; // now immutable
8686
8787 point.y = 6; // this causes an error
8888}
@@ -121,27 +121,24 @@ let point = Point3d { z: 1, x: 2, .. origin };
121121# Tuple structs
122122
123123Rust has another data type that’s like a hybrid between a [ tuple] [ tuple ] and a
124- ` struct ` , called a ‘tuple struct’. Tuple structs have a name, but
125- their fields don’t:
124+ ` struct ` , called a ‘tuple struct’. Tuple structs have a name, but their fields
125+ don't. They are declared with the ` struct ` keyword, and then with a name
126+ followed by a tuple:
127+
128+ [ tuple ] : primitive-types.html#tuples
126129
127130``` rust
128131struct Color (i32 , i32 , i32 );
129132struct Point (i32 , i32 , i32 );
130- ```
131133
132- [ tuple ] : primitive-types.html#tuples
133-
134- These two will not be equal, even if they have the same values:
135-
136- ``` rust
137- # struct Color (i32 , i32 , i32 );
138- # struct Point (i32 , i32 , i32 );
139134let black = Color (0 , 0 , 0 );
140135let origin = Point (0 , 0 , 0 );
141136```
137+ Here, ` black ` and ` origin ` are not equal, even though they contain the same
138+ values.
142139
143- It is almost always better to use a ` struct ` than a tuple struct. We would write
144- ` Color ` and ` Point ` like this instead:
140+ It is almost always better to use a ` struct ` than a tuple struct. We
141+ would write ` Color ` and ` Point ` like this instead:
145142
146143``` rust
147144struct Color {
@@ -157,13 +154,14 @@ struct Point {
157154}
158155```
159156
160- Now, we have actual names, rather than positions. Good names are important,
161- and with a ` struct ` , we have actual names.
157+ Good names are important, and while values in a tuple struct can be
158+ referenced with dot notation as well, a ` struct ` gives us actual names,
159+ rather than positions.
162160
163- There _ is_ one case when a tuple struct is very useful, though, and that’s a
164- tuple struct with only one element. We call this the ‘newtype’ pattern, because
165- it allows you to create a new type, distinct from that of its contained value
166- and expressing its own semantic meaning:
161+ There _ is_ one case when a tuple struct is very useful, though, and that is when
162+ it has only one element. We call this the ‘newtype’ pattern, because
163+ it allows you to create a new type that is distinct from its contained value
164+ and also expresses its own semantic meaning:
167165
168166``` rust
169167struct Inches (i32 );
0 commit comments