Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help


x-i18n: generated_at: “2026-03-01T13:57:53Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 8ded9a736f59d44aa6dc9c9121b503db8ef1d3e2eae8db5be83c04180e3d618f source_path: ch08-01-vectors.md workflow: 16

使用向量存储列表 (Storing Lists of Values with Vectors)

Storing Lists of Values with Vectors

我们要看的第一种集合类型是 Vec<T>,也称为“向量 (vector)”。向量允许你在单个数据结构中存储多个值,这些值在内存中彼此相邻放置。向量只能存储相同类型的值。当你有一个项列表时,例如文件中的文本行或购物车中物品的价格,它们非常有用。

The first collection type we’ll look at is Vec<T>, also known as a vector. Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory. Vectors can only store values of the same type. They are useful when you have a list of items, such as the lines of text in a file or the prices of items in a shopping cart.

创建新向量 (Creating a New Vector)

Creating a New Vector

要创建一个新的空向量,我们调用 Vec::new 函数,如示例 8-1 所示。

To create a new, empty vector, we call the Vec::new function, as shown in Listing 8-1.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}}
}

注意这里我们添加了类型注解。因为我们还没有向这个向量中插入任何值,Rust 不知道我们打算存储哪种类型的元素。这是一个重点。向量是使用泛型实现的;我们将在第 10 章介绍如何在你自己的类型中使用泛型。目前,只需知道标准库提供的 Vec<T> 类型可以持有任何类型。当我们创建一个用于持有特定类型的向量时,可以在尖括号内指定该类型。在示例 8-1 中,我们告诉 Rust v 中的 Vec<T> 将持有 i32 类型的元素。

Note that we added a type annotation here. Because we aren’t inserting any values into this vector, Rust doesn’t know what kind of elements we intend to store. This is an important point. Vectors are implemented using generics; we’ll cover how to use generics with your own types in Chapter 10. For now, know that the Vec<T> type provided by the standard library can hold any type. When we create a vector to hold a specific type, we can specify the type within angle brackets. In Listing 8-1, we’ve told Rust that the Vec<T> in v will hold elements of the i32 type.

更多时候,你会创建一个带有初始值的 Vec<T>,而 Rust 会推断你想要存储的值的类型,所以你很少需要做这种类型注解。Rust 方便地提供了 vec! 宏,它会创建一个包含你所提供的值的新向量。示例 8-2 创建了一个新的 Vec<i32>,其中包含值 123。整数类型是 i32,因为正如我们在第 3 章“数据类型”部分讨论的那样,它是默认的整数类型。

More often, you’ll create a Vec<T> with initial values, and Rust will infer the type of value you want to store, so you rarely need to do this type annotation. Rust conveniently provides the vec! macro, which will create a new vector that holds the values you give it. Listing 8-2 creates a new Vec<i32> that holds the values 1, 2, and 3. The integer type is i32 because that’s the default integer type, as we discussed in the “Data Types” section of Chapter 3.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-02/src/main.rs:here}}
}

因为我们提供了初始的 i32 值,Rust 可以推断出 v 的类型是 Vec<i32>,因此不需要类型注解。接下来,我们将看看如何修改向量。

Because we’ve given initial i32 values, Rust can infer that the type of v is Vec<i32>, and the type annotation isn’t necessary. Next, we’ll look at how to modify a vector.

更新向量 (Updating a Vector)

Updating a Vector

要创建一个向量并向其中添加元素,我们可以使用 push 方法,如示例 8-3 所示。

To create a vector and then add elements to it, we can use the push method, as shown in Listing 8-3.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}}
}

正如我们在第 3 章讨论的任何变量一样,如果我们希望能够改变它的值,我们需要使用 mut 关键字使其可变。我们在其中放置的数字都是 i32 类型,Rust 会根据数据推断出这一点,所以我们不需要 Vec<i32> 注解。

As with any variable, if we want to be able to change its value, we need to make it mutable using the mut keyword, as discussed in Chapter 3. The numbers we place inside are all of type i32, and Rust infers this from the data, so we don’t need the Vec<i32> annotation.

读取向量元素 (Reading Elements of Vectors)

Reading Elements of Vectors

有两种引用存储在向量中的值的方法:通过索引或使用 get 方法。在接下来的示例中,为了更加清晰,我们对这些函数返回的值的类型进行了注解。

There are two ways to reference a value stored in a vector: via indexing or by using the get method. In the following examples, we’ve annotated the types of the values that are returned from these functions for extra clarity.

示例 8-4 展示了访问向量中值的两种方法,即使用索引语法和 get 方法。

Listing 8-4 shows both methods of accessing a value in a vector, with indexing syntax and the get method.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}}
}

注意这里的一些细节。我们使用索引值 2 来获取第三个元素,因为向量是按数字索引的,从零开始。使用 &[] 会给我们一个该索引处元素的引用。当我们使用带有作为参数传递的索引的 get 方法时,我们会得到一个可以与 match 一起使用的 Option<&T>

Note a few details here. We use the index value of 2 to get the third element because vectors are indexed by number, starting at zero. Using & and [] gives us a reference to the element at the index value. When we use the get method with the index passed as an argument, we get an Option<&T> that we can use with match.

Rust 提供这两种引用元素的方式,以便你可以选择在尝试使用超出现有元素范围的索引值时程序的行为。举个例子,让我们看看当我们有一个包含五个元素的向量,然后尝试通过每种技术访问索引 100 处的元素时会发生什么,如示例 8-5 所示。

Rust provides these two ways to reference an element so that you can choose how the program behaves when you try to use an index value outside the range of existing elements. As an example, let’s see what happens when we have a vector of five elements and then we try to access an element at index 100 with each technique, as shown in Listing 8-5.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}}
}

当我们运行这段代码时,第一种 [] 方法将导致程序恐慌 (panic),因为它引用了一个不存在的元素。当你希望程序在尝试访问超出向量末尾的元素时崩溃时,最好使用此方法。

When we run this code, the first [] method will cause the program to panic because it references a nonexistent element. This method is best used when you want your program to crash if there’s an attempt to access an element past the end of the vector.

get 方法接收到一个向量外部的索引时,它会返回 None 而不会恐慌。如果你有时可能会访问超出向量范围的元素(在正常情况下),你可以使用此方法。正如在第 6 章中所讨论的,你的代码随后将拥有逻辑来处理拥有 Some(&element)None 的情况。例如,索引可能来自一个输入数字的人。如果他们不小心输入了一个太大的数字而程序得到了一个 None 值,你可以告诉用户当前向量中有多少项,并给他们另一次输入有效值的机会。这会比由于打字错误而导致程序崩溃更用户友好!

When the get method is passed an index that is outside the vector, it returns None without panicking. You would use this method if accessing an element beyond the range of the vector may happen occasionally under normal circumstances. Your code will then have logic to handle having either Some(&element) or None, as discussed in Chapter 6. For example, the index could be coming from a person entering a number. If they accidentally enter a number that’s too large and the program gets a None value, you could tell the user how many items are in the current vector and give them another chance to enter a valid value. That would be more user-friendly than crashing the program due to a typo!

当程序拥有一个有效引用时,借用检查器会强制执行所有权和借用规则(在第 4 章中介绍),以确保此引用以及对向量内容的任何其他引用保持有效。回想一下规则:在同一个作用域内不能同时拥有可变引用和不可变引用。该规则适用于示例 8-6,其中我们持有一个对向量第一个元素的不可变引用,并尝试向末尾添加一个元素。如果我们稍后在函数中也尝试引用该元素,此程序将无法工作。

When the program has a valid reference, the borrow checker enforces the ownership and borrowing rules (covered in Chapter 4) to ensure that this reference and any other references to the contents of the vector remain valid. Recall the rule that states you can’t have mutable and immutable references in the same scope. That rule applies in Listing 8-6, where we hold an immutable reference to the first element in a vector and try to add an element to the end. This program won’t work if we also try to refer to that element later in the function.

{{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}}

编译这段代码将产生以下错误:

Compiling this code will result in this error:

{{#include ../listings/ch08-common-collections/listing-08-06/output.txt}}

示例 8-6 中的代码看起来似乎应该可以工作:为什么第一个元素的引用要关心向量末尾的更改呢?这个错误是由于向量的工作方式造成的:因为向量在内存中将值相邻放置,如果向量当前存储的位置没有足够的空间将所有元素相邻放置,那么在向量末尾添加新元素可能需要分配新内存并将旧元素复制到新空间。在这种情况下,对第一个元素的引用将指向已释放的内存。借用规则防止程序陷入这种情况。

The code in Listing 8-6 might look like it should work: Why should a reference to the first element care about changes at the end of the vector? This error is due to the way vectors work: Because vectors put the values next to each other in memory, adding a new element onto the end of the vector might require allocating new memory and copying the old elements to the new space, if there isn’t enough room to put all the elements next to each other where the vector is currently stored. In that case, the reference to the first element would be pointing to deallocated memory. The borrowing rules prevent programs from ending up in that situation.

注意:有关 Vec<T> 类型实现细节的更多信息,请参阅 “The Rustonomicon”

Note: For more on the implementation details of the Vec<T> type, see “The Rustonomicon”.

遍历向量中的值 (Iterating Over the Values in a Vector)

Iterating Over the Values in a Vector

要依次访问向量中的每个元素,我们会遍历所有元素,而不是使用索引一次访问一个。示例 8-7 展示了如何使用 for 循环获取 i32 值向量中每个元素的不可变引用并打印它们。

To access each element in a vector in turn, we would iterate through all of the elements rather than use indices to access one at a time. Listing 8-7 shows how to use a for loop to get immutable references to each element in a vector of i32 values and print them.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}}
}

我们也可以遍历可变向量中每个元素的可变引用,以便对所有元素进行更改。示例 8-8 中的 for 循环将给每个元素加 50

We can also iterate over mutable references to each element in a mutable vector in order to make changes to all the elements. The for loop in Listing 8-8 will add 50 to each element.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}}
}

为了更改可变引用所指向的值,在我们可以使用 += 运算符之前,必须使用 * 解引用运算符来获取 i 中的值。我们将在第 15 章的“通过引用获取值”部分更多地讨论解引用运算符。

To change the value that the mutable reference refers to, we have to use the * dereference operator to get to the value in i before we can use the += operator. We’ll talk more about the dereference operator in the “Following the Reference to the Value” section of Chapter 15.

由于借用检查器的规则,无论是不可变地还是可变地遍历向量都是安全的。如果我们尝试在示例 8-7 和示例 8-8 的 for 循环体中插入或删除项,我们将得到类似于示例 8-6 代码中得到的编译器错误。for 循环持有的对向量的引用防止了对整个向量的同时修改。

Iterating over a vector, whether immutably or mutably, is safe because of the borrow checker’s rules. If we attempted to insert or remove items in the for loop bodies in Listing 8-7 and Listing 8-8, we would get a compiler error similar to the one we got with the code in Listing 8-6. The reference to the vector that the for loop holds prevents simultaneous modification of the whole vector.

使用枚举存储多种类型 (Using an Enum to Store Multiple Types)

Using an Enum to Store Multiple Types

向量只能存储相同类型的值。这可能会很不方便;确实有需要存储不同类型项列表的用例。幸运的是,枚举的变体都定义在相同的枚举类型下,所以当我们希望用一种类型来代表不同类型的元素时,我们可以定义并使用一个枚举!

Vectors can only store values that are of the same type. This can be inconvenient; there are definitely use cases for needing to store a list of items of different types. Fortunately, the variants of an enum are defined under the same enum type, so when we need one type to represent elements of different types, we can define and use an enum!

例如,假设我们想要从电子表格的一行中获取值,其中该行的一些列包含整数,一些包含浮点数,还有一些包含字符串。我们可以定义一个枚举,其变体将持有不同的值类型,并且所有的枚举变体都将被视为相同的类型:即该枚举的类型。然后,我们可以创建一个向量来持有该枚举,从而最终持有不同的类型。我们在示例 8-9 中演示了这一点。

For example, say we want to get values from a row in a spreadsheet in which some of the columns in the row contain integers, some floating-point numbers, and some strings. We can define an enum whose variants will hold the different value types, and all the enum variants will be considered the same type: that of the enum. Then, we can create a vector to hold that enum and so, ultimately, hold different types. We’ve demonstrated this in Listing 8-9.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-09/src/main.rs:here}}
}

Rust 需要在编译时知道向量中将会有哪些类型,以便它准确知道在堆上存储每个元素需要多少内存。我们还必须明确这个向量中允许哪些类型。如果 Rust 允许向量持有任何类型,那么一个或多个类型可能会对向量元素执行的操作导致错误。结合使用枚举和 match 表达式意味着 Rust 将在编译时确保处理了每种可能的情况,如第 6 章所述。

Rust needs to know what types will be in the vector at compile time so that it knows exactly how much memory on the heap will be needed to store each element. We must also be explicit about what types are allowed in this vector. If Rust allowed a vector to hold any type, there would be a chance that one or more of the types would cause errors with the operations performed on the elements of the vector. Using an enum plus a match expression means that Rust will ensure at compile time that every possible case is handled, as discussed in Chapter 6.

如果你在运行时不知道程序将获取哪些穷尽的类型集来存储在向量中,枚举技术将行不通。相反,你可以使用特征对象(trait object),我们将在第 18 章介绍它。

If you don’t know the exhaustive set of types a program will get at runtime to store in a vector, the enum technique won’t work. Instead, you can use a trait object, which we’ll cover in Chapter 18.

既然我们已经讨论了向量的一些最常见用法,请务必查看标准库为 Vec<T> 定义的所有许多有用方法的 API 文档。例如,除了 push 之外,还有一个 pop 方法可以移除并返回最后一个元素。

Now that we’ve discussed some of the most common ways to use vectors, be sure to review the API documentation for all of the many useful methods defined on Vec<T> by the standard library. For example, in addition to push, a pop method removes and returns the last element.

丢弃向量也会丢弃其元素 (Dropping a Vector Drops Its Elements)

Dropping a Vector Drops Its Elements

像任何其他 struct 一样,向量在超出作用域时会被释放,如示例 8-10 中标注的那样。

Like any other struct, a vector is freed when it goes out of scope, as annotated in Listing 8-10.

#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-10/src/main.rs:here}}
}

当向量被丢弃时,它的所有内容也都会被丢弃,这意味着它持有的整数将被清理。借用检查器确保任何对向量内容的引用仅在向量本身有效时才被使用。

When the vector gets dropped, all of its contents are also dropped, meaning the integers it holds will be cleaned up. The borrow checker ensures that any references to contents of a vector are only used while the vector itself is valid.

让我们继续学习下一种集合类型:String

Let’s move on to the next collection type: String!