Skip to content

Commit

Permalink
Descripe tuple indexing in TRPL
Browse files Browse the repository at this point in the history
FIxes #23962
  • Loading branch information
steveklabnik committed Apr 17, 2015
1 parent dc4554a commit 525a146
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/doc/trpl/primitive-types.md
Expand Up @@ -216,6 +216,18 @@ In systems programming languages, strings are a bit more complex than in other
languages. For now, just read `&str` as a *string slice*, and we’ll learn more
soon.

You can assign one tuple into another, if they have the same contained types
and [arity]. Tuples have the same arity when they have the same length.

[arity]: glossary.html#arity

```rust
let mut x = (1, 2); // x: (i32, i32)
let y = (2, 3); // y: (i32, i32)

x = y;
```

You can access the fields in a tuple through a *destructuring let*. Here’s
an example:

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

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

There are also a few things you can do with a tuple as a whole, without
destructuring. You can assign one tuple into another, if they have the same
contained types and [arity]. Tuples have the same arity when they have the same
length.
## Tuple Indexing

You can also access fields of a tuple with indexing syntax:

[arity]: glossary.html#arity

```rust
let mut x = (1, 2); // x: (i32, i32)
let y = (2, 3); // y: (i32, i32)
let tuple = (1, 2, 3);

x = y;
let x = tuple.0;
let y = tuple.1;
let z = tuple.2;

println!("x is {}", x);
```

Like array indexing, it starts at zero, but unlike array indexing, it uses a
`.`, rather than `[]`s.

You can find more documentation for tuples [in the standard library
documentation][tuple].

Expand Down

0 comments on commit 525a146

Please sign in to comment.