@@ -216,6 +216,18 @@ In systems programming languages, strings are a bit more complex than in other
216
216
languages. For now, just read ` &str ` as a * string slice* , and we’ll learn more
217
217
soon.
218
218
219
+ You can assign one tuple into another, if they have the same contained types
220
+ and [ arity] . Tuples have the same arity when they have the same length.
221
+
222
+ [ arity ] : glossary.html#arity
223
+
224
+ ``` rust
225
+ let mut x = (1 , 2 ); // x: (i32, i32)
226
+ let y = (2 , 3 ); // y: (i32, i32)
227
+
228
+ x = y ;
229
+ ```
230
+
219
231
You can access the fields in a tuple through a * destructuring let* . Here’s
220
232
an example:
221
233
@@ -235,20 +247,24 @@ or "breaks up," the tuple, and assigns the bits to three bindings.
235
247
236
248
This pattern is very powerful, and we’ll see it repeated more later.
237
249
238
- There are also a few things you can do with a tuple as a whole, without
239
- destructuring. You can assign one tuple into another, if they have the same
240
- contained types and [ arity] . Tuples have the same arity when they have the same
241
- length.
250
+ ## Tuple Indexing
251
+
252
+ You can also access fields of a tuple with indexing syntax:
242
253
243
- [ arity ] : glossary.html#arity
244
254
245
255
``` rust
246
- let mut x = (1 , 2 ); // x: (i32, i32)
247
- let y = (2 , 3 ); // y: (i32, i32)
256
+ let tuple = (1 , 2 , 3 );
248
257
249
- x = y ;
258
+ let x = tuple . 0 ;
259
+ let y = tuple . 1 ;
260
+ let z = tuple . 2 ;
261
+
262
+ println! (" x is {}" , x );
250
263
```
251
264
265
+ Like array indexing, it starts at zero, but unlike array indexing, it uses a
266
+ ` . ` , rather than ` [] ` s.
267
+
252
268
You can find more documentation for tuples [ in the standard library
253
269
documentation] [ tuple ] .
254
270
0 commit comments