]> git.proxmox.com Git - rustc.git/blame - src/doc/book/2018-edition/src/ch08-01-vectors.md
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / doc / book / 2018-edition / src / ch08-01-vectors.md
CommitLineData
83c7162d
XL
1## Storing Lists of Values with Vectors
2
3The first collection type we’ll look at is `Vec<T>`, also known as a *vector*.
4Vectors allow you to store more than one value in a single data structure that
5puts all the values next to each other in memory. Vectors can only store values
6of the same type. They are useful when you have a list of items, such as the
7lines of text in a file or the prices of items in a shopping cart.
8
9### Creating a New Vector
10
11To create a new, empty vector, we can call the `Vec::new` function, as shown in
12Listing 8-1:
13
14```rust
15let v: Vec<i32> = Vec::new();
16```
17
18<span class="caption">Listing 8-1: Creating a new, empty vector to hold values
19of type `i32`</span>
20
21Note that we added a type annotation here. Because we aren’t inserting any
22values into this vector, Rust doesn’t know what kind of elements we intend to
23store. This is an important point. Vectors are implemented using generics;
24we’ll cover how to use generics with your own types in Chapter 10. For now,
25know that the `Vec<T>` type provided by the standard library can hold any type,
26and when a specific vector holds a specific type, the type is specified within
27angle brackets. In Listing 8-1, we’ve told Rust that the `Vec<T>` in `v` will
28hold elements of the `i32` type.
29
30In more realistic code, Rust can often infer the type of value you want to
31store once you insert values, so you rarely need to do this type annotation.
32It’s more common to create a `Vec<T>` that has initial values, and Rust
33provides the `vec!` macro for convenience. The macro will create a new vector
34that holds the values you give it. Listing 8-2 creates a new `Vec<i32>` that
35holds the values `1`, `2`, and `3`:
36
37```rust
38let v = vec![1, 2, 3];
39```
40
41<span class="caption">Listing 8-2: Creating a new vector containing
42values</span>
43
44Because we’ve given initial `i32` values, Rust can infer that the type of `v`
45is `Vec<i32>`, and the type annotation isn’t necessary. Next, we’ll look at how
46to modify a vector.
47
48### Updating a Vector
49
50To create a vector and then add elements to it, we can use the `push` method,
51as shown in Listing 8-3:
52
53```rust
54let mut v = Vec::new();
55
56v.push(5);
57v.push(6);
58v.push(7);
59v.push(8);
60```
61
62<span class="caption">Listing 8-3: Using the `push` method to add values to a
63vector</span>
64
65As with any variable, if we want to be able to change its value, we need to
66make it mutable using the `mut` keyword, as discussed in Chapter 3. The numbers
67we place inside are all of type `i32`, and Rust infers this from the data, so
68we don’t need the `Vec<i32>` annotation.
69
70### Dropping a Vector Drops Its Elements
71
72Like any other `struct`, a vector is freed when it goes out of scope, as
73annotated in Listing 8-4:
74
75```rust
76{
77 let v = vec![1, 2, 3, 4];
78
79 // do stuff with v
80
81} // <- v goes out of scope and is freed here
82```
83
84<span class="caption">Listing 8-4: Showing where the vector and its elements
85are dropped</span>
86
87When the vector gets dropped, all of its contents are also dropped, meaning
88those integers it holds will be cleaned up. This may seem like a
89straightforward point but can get a bit more complicated when you start to
90introduce references to the elements of the vector. Let’s tackle that next!
91
92### Reading Elements of Vectors
93
94Now that you know how to create, update, and destroy vectors, knowing how to
95read their contents is a good next step. There are two ways to reference a
96value stored in a vector. In the examples, we’ve annotated the types of the
97values that are returned from these functions for extra clarity.
98
94b46f34
XL
99Listing 8-5 shows the method of accessing a value in a vector with
100indexing syntax:
83c7162d
XL
101
102```rust
103let v = vec![1, 2, 3, 4, 5];
104
105let third: &i32 = &v[2];
83c7162d
XL
106```
107
94b46f34
XL
108<span class="caption">Listing 8-5: Using indexing syntax to
109access an item in a vector</span>
110
111Listing 8-6 shows the method of accessing a value in a vector, with
112the `get` method:
113
114```rust
115let v = vec![1, 2, 3, 4, 5];
116let v_index = 2;
117
118match v.get(v_index) {
119 Some(_) => { println!("Reachable element at index: {}", v_index); },
120 None => { println!("Unreachable element at index: {}", v_index); }
121}
122```
123
124<span class="caption">Listing 8-6: Using the `get` method to
83c7162d
XL
125access an item in a vector</span>
126
127Note two details here. First, we use the index value of `2` to get the third
128element: vectors are indexed by number, starting at zero. Second, the two ways
129to get the third element are by using `&` and `[]`, which gives us a reference,
130or by using the `get` method with the index passed as an argument, which gives
131us an `Option<&T>`.
132
133Rust has two ways to reference an element so you can choose how the program
134behaves when you try to use an index value that the vector doesn’t have an
135element for. As an example, let’s see what a program will do if it has a vector
136that holds five elements and then tries to access an element at index 100, as
94b46f34 137shown in Listing 8-7:
83c7162d
XL
138
139```rust,should_panic
140let v = vec![1, 2, 3, 4, 5];
141
142let does_not_exist = &v[100];
143let does_not_exist = v.get(100);
144```
145
94b46f34 146<span class="caption">Listing 8-7: Attempting to access the element at index
83c7162d
XL
147100 in a vector containing five elements</span>
148
149When we run this code, the first `[]` method will cause the program to panic
150because it references a nonexistent element. This method is best used when you
151want your program to crash if there’s an attempt to access an element past the
152end of the vector.
153
154When the `get` method is passed an index that is outside the vector, it returns
155`None` without panicking. You would use this method if accessing an element
156beyond the range of the vector happens occasionally under normal circumstances.
157Your code will then have logic to handle having either `Some(&element)` or
158`None`, as discussed in Chapter 6. For example, the index could be coming from
159a person entering a number. If they accidentally enter a number that’s too
160large and the program gets a `None` value, you could tell the user how many
161items are in the current vector and give them another chance to enter a valid
162value. That would be more user-friendly than crashing the program due to a typo!
163
164When the program has a valid reference, the borrow checker enforces the
165ownership and borrowing rules (covered in Chapter 4) to ensure this reference
166and any other references to the contents of the vector remain valid. Recall the
167rule that states you can’t have mutable and immutable references in the same
94b46f34 168scope. That rule applies in Listing 8-8, where we hold an immutable reference to
83c7162d
XL
169the first element in a vector and try to add an element to the end, which won’t
170work:
171
172```rust,ignore
173let mut v = vec![1, 2, 3, 4, 5];
174
175let first = &v[0];
176
177v.push(6);
178```
179
94b46f34 180<span class="caption">Listing 8-8: Attempting to add an element to a vector
83c7162d
XL
181while holding a reference to an item</span>
182
183Compiling this code will result in this error:
184
185```text
186error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
187 -->
188 |
1894 | let first = &v[0];
190 | - immutable borrow occurs here
1915 |
1926 | v.push(6);
193 | ^ mutable borrow occurs here
1947 |
1958 | }
196 | - immutable borrow ends here
197```
198
94b46f34 199The code in Listing 8-8 might look like it should work: why should a reference
83c7162d
XL
200to the first element care about what changes at the end of the vector? This
201error is due to the way vectors work: adding a new element onto the end of the
202vector might require allocating new memory and copying the old elements to the
203new space, if there isn’t enough room to put all the elements next to each
204other where the vector currently is. In that case, the reference to the first
205element would be pointing to deallocated memory. The borrowing rules prevent
206programs from ending up in that situation.
207
208> Note: For more on the implementation details of the `Vec<T>` type, see “The
209> Rustonomicon” at https://doc.rust-lang.org/stable/nomicon/vec.html.
210
211### Iterating over the Values in a Vector
212
213If we want to access each element in a vector in turn, we can iterate through
214all of the elements rather than use indexes to access one at a time. Listing
94b46f34 2158-9 shows how to use a `for` loop to get immutable references to each element
83c7162d
XL
216in a vector of `i32` values and print them:
217
218```rust
219let v = vec![100, 32, 57];
220for i in &v {
221 println!("{}", i);
222}
223```
224
94b46f34 225<span class="caption">Listing 8-9: Printing each element in a vector by
83c7162d
XL
226iterating over the elements using a `for` loop</span>
227
228We can also iterate over mutable references to each element in a mutable vector
94b46f34 229in order to make changes to all the elements. The `for` loop in Listing 8-10
83c7162d
XL
230will add `50` to each element:
231
232```rust
233let mut v = vec![100, 32, 57];
234for i in &mut v {
235 *i += 50;
236}
237```
238
94b46f34 239<span class="caption">Listing 8-10: Iterating over mutable references to
83c7162d
XL
240elements in a vector</span>
241
242To change the value that the mutable reference refers to, we have to use the
243dereference operator (`*`) to get to the value in `i` before we can use the
94b46f34 244`+=` operator . We'll talk more about `*` in Chapter 15.
83c7162d
XL
245
246### Using an Enum to Store Multiple Types
247
248At the beginning of this chapter, we said that vectors can only store values
249that are the same type. This can be inconvenient; there are definitely use
250cases for needing to store a list of items of different types. Fortunately, the
251variants of an enum are defined under the same enum type, so when we need to
252store elements of a different type in a vector, we can define and use an enum!
253
254For example, say we want to get values from a row in a spreadsheet in which
255some of the columns in the row contain integers, some floating-point numbers,
256and some strings. We can define an enum whose variants will hold the different
257value types, and then all the enum variants will be considered the same type:
258that of the enum. Then we can create a vector that holds that enum and so,
94b46f34 259ultimately, holds different types. We’ve demonstrated this in Listing 8-11:
83c7162d
XL
260
261```rust
262enum SpreadsheetCell {
263 Int(i32),
264 Float(f64),
265 Text(String),
266}
267
268let row = vec![
269 SpreadsheetCell::Int(3),
270 SpreadsheetCell::Text(String::from("blue")),
271 SpreadsheetCell::Float(10.12),
272];
273```
274
94b46f34 275<span class="caption">Listing 8-11: Defining an `enum` to store values of
83c7162d
XL
276different types in one vector</span>
277
278Rust needs to know what types will be in the vector at compile time so it knows
279exactly how much memory on the heap will be needed to store each element. A
280secondary advantage is that we can be explicit about what types are allowed in
281this vector. If Rust allowed a vector to hold any type, there would be a chance
282that one or more of the types would cause errors with the operations performed
283on the elements of the vector. Using an enum plus a `match` expression means
284that Rust will ensure at compile time that every possible case is handled, as
285discussed in Chapter 6.
286
287When you’re writing a program, if you don’t know the exhaustive set of types
288the program will get at runtime to store in a vector, the enum technique won’t
289work. Instead, you can use a trait object, which we’ll cover in Chapter 17.
290
291Now that we’ve discussed some of the most common ways to use vectors, be sure
292to review the API documentation for all the many useful methods defined on
293`Vec<T>` by the standard library. For example, in addition to `push`, a `pop`
294method removes and returns the last element. Let’s move on to the next
295collection type: `String`!