Rollup merge of #28765 - steveklabnik:gh28693, r=nikomatsakis

Fixes #28693
This commit is contained in:
Steve Klabnik 2015-09-30 14:51:55 -04:00
commit 15ee0e908c
1 changed files with 29 additions and 0 deletions

View File

@ -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]`.
Its 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];
// doesnt
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
```
Theres 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