]> git.proxmox.com Git - rustc.git/blame - src/doc/book/vectors.md
Imported Upstream version 1.7.0+dfsg1
[rustc.git] / src / doc / book / vectors.md
CommitLineData
9346a6ac
AL
1% Vectors
2
bd371182
AL
3A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard
4library type [`Vec<T>`][vec]. The `T` means that we can have vectors
5of any type (see the chapter on [generics][generic] for more).
6Vectors always allocate their data on the heap.
7You can create them with the `vec!` macro:
8
9```rust
10let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32>
9346a6ac
AL
11```
12
bd371182
AL
13(Notice that unlike the `println!` macro we’ve used in the past, we use square
14brackets `[]` with `vec!` macro. Rust allows you to use either in either situation,
9346a6ac
AL
15this is just convention.)
16
bd371182 17There’s an alternate form of `vec!` for repeating an initial value:
9346a6ac 18
62682a34 19```rust
9346a6ac
AL
20let v = vec![0; 10]; // ten zeroes
21```
22
bd371182
AL
23## Accessing elements
24
25To get the value at a particular index in the vector, we use `[]`s:
26
27```rust
28let v = vec![1, 2, 3, 4, 5];
29
30println!("The third element of v is {}", v[2]);
31```
32
33The indices count from `0`, so the third element is `v[2]`.
34
b039eaaf
SL
35It’s also important to note that you must index with the `usize` type:
36
37```ignore
38let v = vec![1, 2, 3, 4, 5];
39
40let i: usize = 0;
41let j: i32 = 0;
42
43// works
44v[i];
45
46// doesn’t
47v[j];
48```
49
50Indexing with a non-`usize` type gives an error that looks like this:
51
52```text
53error: the trait `core::ops::Index<i32>` is not implemented for the type
54`collections::vec::Vec<_>` [E0277]
55v[j];
56^~~~
57note: the type `collections::vec::Vec<_>` cannot be indexed by `i32`
58error: aborting due to previous error
59```
60
61There’s a lot of punctuation in that message, but the core of it makes sense:
62you cannot index with an `i32`.
63
9cc50fc6
SL
64## Out-of-bounds Access
65
66If you try to access an index that doesn’t exist:
67
68```ignore
69let v = vec![1, 2, 3];
70println!("Item 7 is {}", v[7]);
71```
72
73then the current thread will [panic] with a message like this:
74
75```text
76thread '<main>' panicked at 'index out of bounds: the len is 3 but the index is 7'
77```
78
79If you want to handle out-of-bounds errors without panicking, you can use
80methods like [`get`][get] or [`get_mut`][get_mut] that return `None` when
81given an invalid index:
82
83```rust
84let v = vec![1, 2, 3];
85match v.get(7) {
86 Some(x) => println!("Item 7 is {}", x),
87 None => println!("Sorry, this vector is too short.")
88}
89```
90
bd371182
AL
91## Iterating
92
93Once you have a vector, you can iterate through its elements with `for`. There
94are three versions:
9346a6ac 95
bd371182
AL
96```rust
97let mut v = vec![1, 2, 3, 4, 5];
9346a6ac 98
bd371182
AL
99for i in &v {
100 println!("A reference to {}", i);
101}
9346a6ac 102
bd371182
AL
103for i in &mut v {
104 println!("A mutable reference to {}", i);
105}
106
107for i in v {
108 println!("Take ownership of the vector and its element {}", i);
109}
9346a6ac
AL
110```
111
bd371182
AL
112Vectors have many more useful methods, which you can read about in [their
113API documentation][vec].
114
115[vec]: ../std/vec/index.html
116[generic]: generics.html
9cc50fc6
SL
117[panic]: concurrency.html#panics
118[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get
119[get_mut]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get_mut