]> git.proxmox.com Git - rustc.git/blame - src/doc/book/src/ch08-01-vectors.md
New upstream version 1.47.0~beta.2+dfsg1
[rustc.git] / src / doc / book / src / ch08-01-vectors.md
CommitLineData
13cf67c4
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
69743fb6 12Listing 8-1.
13cf67c4
XL
13
14```rust
74b04a01 15{{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}}
13cf67c4
XL
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
74b04a01
XL
35holds the values `1`, `2`, and `3`. The integer type is `i32` because that’s
36the default integer type, as we discussed in the [“Data Types”][data-types]<!--
37ignore --> section of Chapter 3.
13cf67c4
XL
38
39```rust
74b04a01 40{{#rustdoc_include ../listings/ch08-common-collections/listing-08-02/src/main.rs:here}}
13cf67c4
XL
41```
42
43<span class="caption">Listing 8-2: Creating a new vector containing
44values</span>
45
46Because we’ve given initial `i32` values, Rust can infer that the type of `v`
47is `Vec<i32>`, and the type annotation isn’t necessary. Next, we’ll look at how
48to modify a vector.
49
50### Updating a Vector
51
52To create a vector and then add elements to it, we can use the `push` method,
69743fb6 53as shown in Listing 8-3.
13cf67c4
XL
54
55```rust
74b04a01 56{{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}}
13cf67c4
XL
57```
58
59<span class="caption">Listing 8-3: Using the `push` method to add values to a
60vector</span>
61
62As with any variable, if we want to be able to change its value, we need to
63make it mutable using the `mut` keyword, as discussed in Chapter 3. The numbers
64we place inside are all of type `i32`, and Rust infers this from the data, so
65we don’t need the `Vec<i32>` annotation.
66
67### Dropping a Vector Drops Its Elements
68
69Like any other `struct`, a vector is freed when it goes out of scope, as
69743fb6 70annotated in Listing 8-4.
13cf67c4
XL
71
72```rust
74b04a01 73{{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}}
13cf67c4
XL
74```
75
76<span class="caption">Listing 8-4: Showing where the vector and its elements
77are dropped</span>
78
79When the vector gets dropped, all of its contents are also dropped, meaning
80those integers it holds will be cleaned up. This may seem like a
81straightforward point but can get a bit more complicated when you start to
82introduce references to the elements of the vector. Let’s tackle that next!
83
84### Reading Elements of Vectors
85
86Now that you know how to create, update, and destroy vectors, knowing how to
87read their contents is a good next step. There are two ways to reference a
88value stored in a vector. In the examples, we’ve annotated the types of the
89values that are returned from these functions for extra clarity.
90
69743fb6
XL
91Listing 8-5 shows both methods of accessing a value in a vector, either with
92indexing syntax or the `get` method.
13cf67c4
XL
93
94```rust
74b04a01 95{{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}}
13cf67c4
XL
96```
97
69743fb6 98<span class="caption">Listing 8-5: Using indexing syntax or the `get` method to
13cf67c4
XL
99access an item in a vector</span>
100
101Note two details here. First, we use the index value of `2` to get the third
102element: vectors are indexed by number, starting at zero. Second, the two ways
103to get the third element are by using `&` and `[]`, which gives us a reference,
104or by using the `get` method with the index passed as an argument, which gives
105us an `Option<&T>`.
106
107Rust has two ways to reference an element so you can choose how the program
108behaves when you try to use an index value that the vector doesn’t have an
109element for. As an example, let’s see what a program will do if it has a vector
110that holds five elements and then tries to access an element at index 100, as
69743fb6 111shown in Listing 8-6.
13cf67c4
XL
112
113```rust,should_panic,panics
74b04a01 114{{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}}
13cf67c4
XL
115```
116
69743fb6 117<span class="caption">Listing 8-6: Attempting to access the element at index
13cf67c4
XL
118100 in a vector containing five elements</span>
119
120When we run this code, the first `[]` method will cause the program to panic
121because it references a nonexistent element. This method is best used when you
122want your program to crash if there’s an attempt to access an element past the
123end of the vector.
124
125When the `get` method is passed an index that is outside the vector, it returns
126`None` without panicking. You would use this method if accessing an element
127beyond the range of the vector happens occasionally under normal circumstances.
128Your code will then have logic to handle having either `Some(&element)` or
129`None`, as discussed in Chapter 6. For example, the index could be coming from
130a person entering a number. If they accidentally enter a number that’s too
131large and the program gets a `None` value, you could tell the user how many
132items are in the current vector and give them another chance to enter a valid
133value. That would be more user-friendly than crashing the program due to a typo!
134
135When the program has a valid reference, the borrow checker enforces the
136ownership and borrowing rules (covered in Chapter 4) to ensure this reference
137and any other references to the contents of the vector remain valid. Recall the
138rule that states you can’t have mutable and immutable references in the same
69743fb6 139scope. That rule applies in Listing 8-7, where we hold an immutable reference to
13cf67c4 140the first element in a vector and try to add an element to the end, which won’t
f035d41b 141work if we also try to refer to that element later in the function:
13cf67c4
XL
142
143```rust,ignore,does_not_compile
74b04a01 144{{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}}
13cf67c4
XL
145```
146
69743fb6 147<span class="caption">Listing 8-7: Attempting to add an element to a vector
13cf67c4
XL
148while holding a reference to an item</span>
149
150Compiling this code will result in this error:
151
f035d41b 152```console
74b04a01 153{{#include ../listings/ch08-common-collections/listing-08-07/output.txt}}
13cf67c4
XL
154```
155
69743fb6 156The code in Listing 8-7 might look like it should work: why should a reference
13cf67c4
XL
157to the first element care about what changes at the end of the vector? This
158error is due to the way vectors work: adding a new element onto the end of the
159vector might require allocating new memory and copying the old elements to the
160new space, if there isn’t enough room to put all the elements next to each
161other where the vector currently is. In that case, the reference to the first
162element would be pointing to deallocated memory. The borrowing rules prevent
163programs from ending up in that situation.
164
74b04a01
XL
165> Note: For more on the implementation details of the `Vec<T>` type, see [“The
166> Rustonomicon”][nomicon].
13cf67c4
XL
167
168### Iterating over the Values in a Vector
169
170If we want to access each element in a vector in turn, we can iterate through
532ac7d7 171all of the elements rather than use indices to access one at a time. Listing
69743fb6
XL
1728-8 shows how to use a `for` loop to get immutable references to each element
173in a vector of `i32` values and print them.
13cf67c4
XL
174
175```rust
74b04a01 176{{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}}
13cf67c4
XL
177```
178
69743fb6 179<span class="caption">Listing 8-8: Printing each element in a vector by
13cf67c4
XL
180iterating over the elements using a `for` loop</span>
181
182We can also iterate over mutable references to each element in a mutable vector
69743fb6
XL
183in order to make changes to all the elements. The `for` loop in Listing 8-9
184will add `50` to each element.
13cf67c4
XL
185
186```rust
74b04a01 187{{#rustdoc_include ../listings/ch08-common-collections/listing-08-09/src/main.rs:here}}
13cf67c4
XL
188```
189
69743fb6 190<span class="caption">Listing 8-9: Iterating over mutable references to
13cf67c4
XL
191elements in a vector</span>
192
193To change the value that the mutable reference refers to, we have to use the
194dereference operator (`*`) to get to the value in `i` before we can use the
9fa01778 195`+=` operator. We’ll talk more about the dereference operator in the
532ac7d7 196[“Following the Pointer to the Value with the Dereference Operator”][deref]
9fa01778 197section of Chapter 15.
13cf67c4
XL
198
199### Using an Enum to Store Multiple Types
200
201At the beginning of this chapter, we said that vectors can only store values
202that are the same type. This can be inconvenient; there are definitely use
203cases for needing to store a list of items of different types. Fortunately, the
204variants of an enum are defined under the same enum type, so when we need to
205store elements of a different type in a vector, we can define and use an enum!
206
207For example, say we want to get values from a row in a spreadsheet in which
208some of the columns in the row contain integers, some floating-point numbers,
209and some strings. We can define an enum whose variants will hold the different
210value types, and then all the enum variants will be considered the same type:
211that of the enum. Then we can create a vector that holds that enum and so,
69743fb6 212ultimately, holds different types. We’ve demonstrated this in Listing 8-10.
13cf67c4
XL
213
214```rust
74b04a01 215{{#rustdoc_include ../listings/ch08-common-collections/listing-08-10/src/main.rs:here}}
13cf67c4
XL
216```
217
69743fb6 218<span class="caption">Listing 8-10: Defining an `enum` to store values of
13cf67c4
XL
219different types in one vector</span>
220
221Rust needs to know what types will be in the vector at compile time so it knows
222exactly how much memory on the heap will be needed to store each element. A
223secondary advantage is that we can be explicit about what types are allowed in
224this vector. If Rust allowed a vector to hold any type, there would be a chance
225that one or more of the types would cause errors with the operations performed
226on the elements of the vector. Using an enum plus a `match` expression means
227that Rust will ensure at compile time that every possible case is handled, as
228discussed in Chapter 6.
229
230When you’re writing a program, if you don’t know the exhaustive set of types
231the program will get at runtime to store in a vector, the enum technique won’t
232work. Instead, you can use a trait object, which we’ll cover in Chapter 17.
233
234Now that we’ve discussed some of the most common ways to use vectors, be sure
3dfed10e 235to review [the API documentation][vec-api] for all the many useful methods defined on
13cf67c4
XL
236`Vec<T>` by the standard library. For example, in addition to `push`, a `pop`
237method removes and returns the last element. Let’s move on to the next
238collection type: `String`!
9fa01778 239
74b04a01
XL
240[data-types]: ch03-02-data-types.html#data-types
241[nomicon]: ../nomicon/vec.html
3dfed10e 242[vec-api]: ../std/vec/struct.Vec.html
9fa01778 243[deref]: ch15-02-deref.html#following-the-pointer-to-the-value-with-the-dereference-operator