Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Descripe tuple indexing in TRPL #24509

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/doc/trpl/primitive-types.md
Original file line number Diff line number Diff line change
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;
```

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this up because it felt weird after the new subheading, but i wanted the heading so we can link to it

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