|
1 | 1 | % Vectors
|
2 | 2 |
|
3 |
| -A *vector* is a dynamic or "growable" array, implemented as the standard |
4 |
| -library type [`Vec<T>`](../std/vec/) (Where `<T>` is a [Generic](./generics.md) |
5 |
| -statement). Vectors always allocate their data on the heap. Vectors are to |
6 |
| -[slices][slices] what [`String`][string] is to `&str`. You can |
7 |
| -create them with the `vec!` macro: |
8 |
| - |
9 |
| -```{rust} |
10 |
| -let v = vec![1, 2, 3]; // v: Vec<i32> |
11 |
| -``` |
| 3 | +A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard |
| 4 | +library type [`Vec<T>`][vec]. That `<T>` is a [generic][generic], meaning we |
| 5 | +can have vectors of any type. Vectors always allocate their data on the heap. |
| 6 | +You can create them with the `vec!` macro: |
12 | 7 |
|
13 |
| -[slices]: primitive-types.html#slices |
14 |
| -[string]: strings.html |
| 8 | +```rust |
| 9 | +let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32> |
| 10 | +``` |
15 | 11 |
|
16 |
| -(Notice that unlike the `println!` macro we've used in the past, we use square |
17 |
| -brackets `[]` with `vec!`. Rust allows you to use either in either situation, |
| 12 | +(Notice that unlike the `println!` macro we’ve used in the past, we use square |
| 13 | +brackets `[]` with `vec!` macro. Rust allows you to use either in either situation, |
18 | 14 | this is just convention.)
|
19 | 15 |
|
20 |
| -There's an alternate form of `vec!` for repeating an initial value: |
| 16 | +There’s an alternate form of `vec!` for repeating an initial value: |
21 | 17 |
|
22 | 18 | ```
|
23 | 19 | let v = vec![0; 10]; // ten zeroes
|
24 | 20 | ```
|
25 | 21 |
|
26 |
| -You can get the length of, iterate over, and subscript vectors just like |
27 |
| -arrays. In addition, (mutable) vectors can grow automatically: |
| 22 | +## Accessing elements |
28 | 23 |
|
29 |
| -```{rust} |
30 |
| -let mut nums = vec![1, 2, 3]; // mut nums: Vec<i32> |
| 24 | +To get the value at a particular index in the vector, we use `[]`s: |
31 | 25 |
|
32 |
| -nums.push(4); |
| 26 | +```rust |
| 27 | +let v = vec![1, 2, 3, 4, 5]; |
33 | 28 |
|
34 |
| -println!("The length of nums is now {}", nums.len()); // Prints 4 |
| 29 | +println!("The third element of v is {}", v[2]); |
35 | 30 | ```
|
36 | 31 |
|
37 |
| -Vectors have many more useful methods. |
| 32 | +The indices count from `0`, so the third element is `v[2]`. |
| 33 | + |
| 34 | +## Iterating |
| 35 | + |
| 36 | +Once you have a vector, you can iterate through its elements with `for`. There |
| 37 | +are three versions: |
| 38 | + |
| 39 | +```rust |
| 40 | +let mut v = vec![1, 2, 3, 4, 5]; |
| 41 | + |
| 42 | +for i in &v { |
| 43 | + println!("A reference to {}", i); |
| 44 | +} |
| 45 | + |
| 46 | +for i in &mut v { |
| 47 | + println!("A mutable reference to {}", i); |
| 48 | +} |
| 49 | + |
| 50 | +for i in v { |
| 51 | + println!("Take ownership of the vector and its element {}", i); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Vectors have many more useful methods, which you can read about in [their |
| 56 | +API documentation][vec]. |
| 57 | + |
| 58 | +[vec]: ../std/vec/index.html |
0 commit comments