@@ -9,7 +9,8 @@ let origin_x = 0;
9
9
let origin_y = 0 ;
10
10
```
11
11
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:
13
14
14
15
``` rust
15
16
struct Point {
@@ -32,7 +33,7 @@ We can create an instance of our `struct` via `let`, as usual, but we use a `key
32
33
value` style syntax to set each field. The order doesn’t need to be the same as
33
34
in the original declaration.
34
35
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
36
37
notation: ` origin.x ` .
37
38
38
39
The values in ` struct ` s are immutable by default, like other bindings in Rust.
@@ -67,9 +68,8 @@ struct Point {
67
68
68
69
Mutability is a property of the binding, not of the structure itself. If you’re
69
70
used 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:
73
73
74
74
``` rust,ignore
75
75
struct Point {
@@ -82,7 +82,7 @@ fn main() {
82
82
83
83
point.x = 5;
84
84
85
- let point = point; // this new binding can’t change now
85
+ let point = point; // now immutable
86
86
87
87
point.y = 6; // this causes an error
88
88
}
@@ -121,27 +121,24 @@ let point = Point3d { z: 1, x: 2, .. origin };
121
121
# Tuple structs
122
122
123
123
Rust 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
126
129
127
130
``` rust
128
131
struct Color (i32 , i32 , i32 );
129
132
struct Point (i32 , i32 , i32 );
130
- ```
131
133
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 );
139
134
let black = Color (0 , 0 , 0 );
140
135
let origin = Point (0 , 0 , 0 );
141
136
```
137
+ Here, ` black ` and ` origin ` are not equal, even though they contain the same
138
+ values.
142
139
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:
145
142
146
143
``` rust
147
144
struct Color {
@@ -157,13 +154,14 @@ struct Point {
157
154
}
158
155
```
159
156
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.
162
160
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:
167
165
168
166
``` rust
169
167
struct Inches (i32 );
0 commit comments