@@ -163,11 +163,48 @@ struct Point(i32, i32, i32);
163
163
let black = Color (0 , 0 , 0 );
164
164
let origin = Point (0 , 0 , 0 );
165
165
```
166
+
166
167
Here, ` black ` and ` origin ` are not equal, even though they contain the same
167
168
values.
168
169
169
- It is almost always better to use a ` struct ` than a tuple struct. We
170
- would write ` Color ` and ` Point ` like this instead:
170
+ The members of a tuple struct may be accessed by dot notation or destructuring
171
+ ` let ` , just like regular tuples:
172
+
173
+ ``` rust
174
+ # struct Color (i32 , i32 , i32 );
175
+ # struct Point (i32 , i32 , i32 );
176
+ # let black = Color (0 , 0 , 0 );
177
+ # let origin = Point (0 , 0 , 0 );
178
+ let black_r = black . 0 ;
179
+ let (_ , origin_y , origin_z ) = origin ;
180
+ ```
181
+
182
+ One case when a tuple struct is very useful is when it has only one element.
183
+ We call this the ‘newtype’ pattern, because it allows you to create a new type
184
+ that is distinct from its contained value and also expresses its own semantic
185
+ meaning:
186
+
187
+ ``` rust
188
+ struct Inches (i32 );
189
+
190
+ let length = Inches (10 );
191
+
192
+ let Inches (integer_length ) = length ;
193
+ println! (" length is {} inches" , integer_length );
194
+ ```
195
+
196
+ As above, you can extract the inner integer type through a destructuring ` let ` .
197
+ In this case, the ` let Inches(integer_length) ` assigns ` 10 ` to ` integer_length ` .
198
+ We could have used dot notation to do the same thing:
199
+
200
+ ``` rust
201
+ # struct Inches (i32 );
202
+ # let length = Inches (10 );
203
+ let integer_length = length . 0 ;
204
+ ```
205
+
206
+ It's always possible to use a ` struct ` than a tuple struct, and can be clearer.
207
+ We would write ` Color ` and ` Point ` like this instead:
171
208
172
209
``` rust
173
210
struct Color {
@@ -187,32 +224,14 @@ Good names are important, and while values in a tuple struct can be
187
224
referenced with dot notation as well, a ` struct ` gives us actual names,
188
225
rather than positions.
189
226
190
- There _ is_ one case when a tuple struct is very useful, though, and that is when
191
- it has only one element. We call this the ‘newtype’ pattern, because
192
- it allows you to create a new type that is distinct from its contained value
193
- and also expresses its own semantic meaning:
194
-
195
- ``` rust
196
- struct Inches (i32 );
197
-
198
- let length = Inches (10 );
199
-
200
- let Inches (integer_length ) = length ;
201
- println! (" length is {} inches" , integer_length );
202
- ```
203
-
204
- As you can see here, you can extract the inner integer type through a
205
- destructuring ` let ` , as with regular tuples. In this case, the
206
- ` let Inches(integer_length) ` assigns ` 10 ` to ` integer_length ` .
207
-
208
227
# Unit-like structs
209
228
210
229
You can define a ` struct ` with no members at all:
211
230
212
231
``` rust
213
- struct Electron ;
232
+ struct Electron {}
214
233
215
- let x = Electron ;
234
+ let x = Electron {} ;
216
235
```
217
236
218
237
Such a ` struct ` is called ‘unit-like’ because it resembles the empty
0 commit comments