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

Mention that you can only index with usize #28765

Merged
merged 1 commit into from
Sep 30, 2015
Merged
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
29 changes: 29 additions & 0 deletions src/doc/trpl/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ println!("The third element of v is {}", v[2]);

The indices count from `0`, so the third element is `v[2]`.

It’s also important to note that you must index with the `usize` type:

```ignore
let v = vec![1, 2, 3, 4, 5];

let i: usize = 0;
let j: i32 = 0;

// works
v[i];

// doesn’t
v[j];
```

Indexing with a non-`usize` type gives an error that looks like this:

```text
error: the trait `core::ops::Index<i32>` is not implemented for the type
`collections::vec::Vec<_>` [E0277]
v[j];
^~~~
note: the type `collections::vec::Vec<_>` cannot be indexed by `i32`
error: aborting due to previous error
```

There’s a lot of punctuation in that message, but the core of it makes sense:
you cannot index with an `i32`.

## Iterating

Once you have a vector, you can iterate through its elements with `for`. There
Expand Down