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