Skip to content

Commit 0131143

Browse files
committed
Rollup merge of #24509 - steveklabnik:gh23962, r=nikomatsakis
FIxes #23962
2 parents 6146eda + dabc486 commit 0131143

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/doc/trpl/primitive-types.md

+24-8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ In systems programming languages, strings are a bit more complex than in other
216216
languages. For now, just read `&str` as a *string slice*, and we’ll learn more
217217
soon.
218218

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+
219231
You can access the fields in a tuple through a *destructuring let*. Here’s
220232
an example:
221233

@@ -235,20 +247,24 @@ or "breaks up," the tuple, and assigns the bits to three bindings.
235247

236248
This pattern is very powerful, and we’ll see it repeated more later.
237249

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:
242253

243-
[arity]: glossary.html#arity
244254

245255
```rust
246-
let mut x = (1, 2); // x: (i32, i32)
247-
let y = (2, 3); // y: (i32, i32)
256+
let tuple = (1, 2, 3);
248257

249-
x = y;
258+
let x = tuple.0;
259+
let y = tuple.1;
260+
let z = tuple.2;
261+
262+
println!("x is {}", x);
250263
```
251264

265+
Like array indexing, it starts at zero, but unlike array indexing, it uses a
266+
`.`, rather than `[]`s.
267+
252268
You can find more documentation for tuples [in the standard library
253269
documentation][tuple].
254270

0 commit comments

Comments
 (0)