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

使用 Vector 存储一系列值

Storing Lists of Values with Vectors

我们要看的第一个集合类型是 Vec<T>,也被称为 vector。Vector 允许你在单个数据结构中存储多个值,这些值在内存中彼此相邻。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.

创建一个新的 Vector

Creating a New Vector

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

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

fn main() {
    let v: Vec<i32> = Vec::new();
}

注意这里我们添加了类型标注。因为我们没有向这个 vector 插入任何值,Rust 不知道我们打算存储哪种类型的元素。这是一个重点。Vector 是使用泛型实现的;我们将在第 10 章中介绍如何在你自己的类型中使用泛型。目前,你只需要知道标准库提供的 Vec<T> 类型可以保存任何类型。当我们创建一个保存特定类型的 vector 时,我们可以在尖括号内指定该类型。在示例 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! 宏,它可以根据你提供的值创建一个新的 vector。示例 8-2 创建了一个新的包含值 123Vec<i32>。整数类型是 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.

fn main() {
    let v = vec![1, 2, 3];
}

因为我们已经给出了初始的 i32 值,Rust 可以推断出 v 的类型是 Vec<i32>,因此类型标注不是必需的。接下来,我们将看看如何修改一个 vector。

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.

更新 Vector

Updating a Vector

要创建一个 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.

fn main() {
    let mut v = Vec::new();

    v.push(5);
    v.push(6);
    v.push(7);
    v.push(8);
}

与任何变量一样,如果我们希望能够更改它的值,我们需要使用 mut 关键字将其设为可变的,如第 3 章所述。我们放入其中的数字都是 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.

读取 Vector 的元素

Reading Elements of Vectors

有两种方式可以引用存储在 vector 中的值:通过索引或使用 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 展示了访问 vector 中值的两种方法,即索引语法和 get 方法。

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

fn main() {
    let v = vec![1, 2, 3, 4, 5];

    let third: &i32 = &v[2];
    println!("The third element is {third}");

    let third: Option<&i32> = v.get(2);
    match third {
        Some(third) => println!("The third element is {third}"),
        None => println!("There is no third element."),
    }
}

注意这里的一些细节。我们使用索引值 2 来获取第三个元素,因为 vector 是通过数字索引的,从零开始。使用 &[] 会得到一个指向该索引值处元素的引用。当我们使用 get 方法并将索引作为参数传递时,我们会得到一个可以用于 matchOption<&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 提供这两种引用元素的方式,以便你可以选择当尝试使用现有元素范围之外的索引值时程序的行为。作为一个例子,让我们看看当我们有一个包含五个元素的 vector,然后尝试使用每种技术访问索引 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.

fn main() {
    let v = vec![1, 2, 3, 4, 5];

    let does_not_exist = &v[100];
    let does_not_exist = v.get(100);
}

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

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 方法被传递一个超出 vector 范围的索引时,它会返回 None 而不发生恐慌。如果在正常情况下偶尔可能会发生访问超出 vector 范围的元素,你应当使用此方法。然后你的代码将拥有处理 Some(&element)None 的逻辑,正如第 6 章中所讨论的那样。例如,索引可能来自人输入的数字。如果他们不小心输入了一个太大的数字,程序得到了一个 None 值,你可以告诉用户当前 vector 中有多少项,并给他们另一次输入有效值的机会。这比因为打错字而导致程序崩溃要对用户更友好!

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 章中介绍),以确保此引用以及对 vector 内容的任何其他引用保持有效。回想一下那个规定你不能在同一作用域内同时拥有可变引用和不可变引用的规则。该规则也适用于示例 8-6,其中我们持有一个对 vector 第一个元素的不可变引用,并尝试在末尾添加一个元素。如果我们稍后在函数中也尝试引用该元素,程序将无法工作。

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.

fn main() {
    let mut v = vec![1, 2, 3, 4, 5];

    let first = &v[0];

    v.push(6);

    println!("The first element is: {first}");
}

编译这段代码会导致如下错误:

Compiling this code will result in this error:

$ cargo run
   Compiling collections v0.1.0 (file:///projects/collections)
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
 --> src/main.rs:6:5
  |
4 |     let first = &v[0];
  |                  - immutable borrow occurs here
5 |
6 |     v.push(6);
  |     ^^^^^^^^^ mutable borrow occurs here
7 |
8 |     println!("The first element is: {first}");
  |                                      ----- immutable borrow later used here

For more information about this error, try `rustc --explain E0502`.
error: could not compile `collections` (bin "collections") due to 1 previous error

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

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”.

遍历 Vector 中的值

Iterating Over the Values in a Vector

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

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.

fn main() {
    let v = vec![100, 32, 57];
    for i in &v {
        println!("{i}");
    }
}

我们还可以遍历可变 vector 中每个元素的可变引用,以便更改所有元素。示例 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.

fn main() {
    let mut v = vec![100, 32, 57];
    for i in &mut v {
        *i += 50;
    }
}

要更改可变引用所指向的值,我们必须使用 * 解引用运算符来获取 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.

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

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

Vector 只能存储相同类型的值。这可能很不方便;在某些用例中确实需要存储不同类型的项目列表。幸运的是,枚举的变体定义在同一个枚举类型下,所以当我们需要一个类型来表示不同类型的元素时,我们可以定义并使用枚举!

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!

例如,假设我们想从电子表格的一行中获取值,其中该行的一些列包含整数,一些包含浮点数,一些包含字符串。我们可以定义一个枚举,其变体将保存不同的值类型,并且所有枚举变体都将被视为相同的类型:即该枚举的类型。然后,我们可以创建一个 vector 来保存该枚举,从而最终保存不同的类型。我们在示例 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.

fn main() {
    enum SpreadsheetCell {
        Int(i32),
        Float(f64),
        Text(String),
    }

    let row = vec![
        SpreadsheetCell::Int(3),
        SpreadsheetCell::Text(String::from("blue")),
        SpreadsheetCell::Float(10.12),
    ];
}

Rust 需要在编译时知道 vector 中将包含哪些类型,以便它确切地知道在堆上存储每个元素需要多少内存。我们也必须显式说明此 vector 中允许哪些类型。如果 Rust 允许 vector 保存任何类型,那么 vector 元素上执行的操作可能会导致一个或多个类型出错。使用枚举加上 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.

如果你不知道程序在运行时会获取哪些详尽的类型集并将其存储在 vector 中,那么枚举技术将不起作用。相反,你可以使用 trait 对象,我们将在第 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.

现在我们已经讨论了一些使用 vector 的最常见方法,请务必查看标准库在 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.

丢弃 Vector 也会丢弃其元素

Dropping a Vector Drops Its Elements

与任何其他 struct 一样,当 vector 超出作用域时会被释放,如示例 8-10 所示。

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

fn main() {
    {
        let v = vec![1, 2, 3, 4];

        // do stuff with v
    } // <- v goes out of scope and is freed here
}

当 vector 被丢弃(drop)时,它的所有内容也会被丢弃,这意味着它保存的整数将被清理。借用检查器确保只有在 vector 本身有效时才使用对 vector 内容的任何引用。

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!