x-i18n: generated_at: “2026-03-01T13:12:43Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 353f08f5f9306cec3bdcac6a9efd3e5c7968264a6fc3f489e1c5abea3b3fbdda source_path: ch02-00-guessing-game-tutorial.md workflow: 16
编写猜数字游戏 (Programming a Guessing Game)
Programming a Guessing Game
让我们通过共同完成一个动手项目来跳入 Rust 的世界!本章将向你展示如何在一个真实程序中使用一些常见的 Rust 概念。你将学习 let、match、方法 (methods)、关联函数 (associated functions)、外部 crate 等内容!在接下来的章节中,我们将更详细地探讨这些想法。在本章中,你只需要练习基础知识。
Let’s jump into Rust by working through a hands-on project together! This chapter introduces you to a few common Rust concepts by showing you how to use them in a real program. You’ll learn about let, match, methods, associated functions, external crates, and more! In the following chapters, we’ll explore these ideas in more detail. In this chapter, you’ll just practice the fundamentals.
我们将实现一个经典的初学者编程问题:猜数字游戏。它的工作原理是:程序将生成一个 1 到 100 之间的随机整数。然后它会提示玩家输入一个猜测。在输入猜测后,程序将指示该猜测是太低还是太高。如果猜测正确,游戏将打印一条恭喜消息并退出。
We’ll implement a classic beginner programming problem: a guessing game. Here’s how it works: The program will generate a random integer between 1 and 100. It will then prompt the player to enter a guess. After a guess is entered, the program will indicate whether the guess is too low or too high. If the guess is correct, the game will print a congratulatory message and exit.
创建一个新项目 (Setting Up a New Project)
Setting Up a New Project
要创建一个新项目,请进入你在第 1 章中创建的 projects 目录,并使用 Cargo 创建一个新项目,如下所示:
To set up a new project, go to the projects directory that you created in Chapter 1 and make a new project using Cargo, like so:
$ cargo new guessing_game
$ cd guessing_game
第一个命令 cargo new 将项目名称 (guessing_game) 作为第一个参数。第二个命令切换到新项目的目录。
The first command, cargo new, takes the name of the project (guessing_game) as the first argument. The second command changes to the new project’s directory.
查看生成的 Cargo.toml 文件:
Look at the generated Cargo.toml file:
文件名:Cargo.toml (Filename: Cargo.toml)
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.toml}}
正如你在第 1 章中所看到的,cargo new 为你生成了一个 “Hello, world!” 程序。查看 src/main.rs 文件:
As you saw in Chapter 1, cargo new generates a “Hello, world!” program for you. Check out the src/main.rs file:
文件名:src/main.rs (Filename: src/main.rs)
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs}}
}
现在让我们使用 cargo run 命令在同一步骤中编译并运行这个 “Hello, world!” 程序:
Now let’s compile this “Hello, world!” program and run it in the same step using the cargo run command:
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt}}
当你需要快速迭代一个项目时,run 命令非常方便,就像我们在这个游戏中要做的那样,在进入下一步之前快速测试每一次迭代。
The run command comes in handy when you need to rapidly iterate on a project, as we’ll do in this game, quickly testing each iteration before moving on to the next one.
重新打开 src/main.rs 文件。你将在这个文件中编写所有的代码。
Reopen the src/main.rs file. You’ll be writing all the code in this file.
处理猜测 (Processing a Guess)
Processing a Guess
猜数字游戏程序的第一部分将询问用户输入,处理该输入,并检查输入是否符合预期形式。首先,我们将允许玩家输入一个猜测。将示例 2-1 中的代码输入到 src/main.rs 中。
The first part of the guessing game program will ask for user input, process that input, and check that the input is in the expected form. To start, we’ll allow the player to input a guess. Enter the code in Listing 2-1 into src/main.rs.
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:all}}
这段代码包含很多信息,让我们逐行过一遍。为了获取用户输入并将结果作为输出打印出来,我们需要将 io 输入/输出库引入作用域。io 库来自标准库,即 std:
This code contains a lot of information, so let’s go over it line by line. To obtain user input and then print the result as output, we need to bring the io input/output library into scope. The io library comes from the standard library, known as std:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:io}}
默认情况下,Rust 会在标准库中定义一组项,并将其引入每个程序的作用域。这组项被称为 prelude (预导入模块),你可以在 标准库文档 中查看其中的所有内容。
By default, Rust has a set of items defined in the standard library that it brings into the scope of every program. This set is called the prelude, and you can see everything in it in the standard library documentation.
如果你想使用的类型不在 prelude 中,你必须使用 use 语句显式地将该类型引入作用域。使用 std::io 库可以为你提供许多有用的功能,包括接受用户输入的能力。
If a type you want to use isn’t in the prelude, you have to bring that type into scope explicitly with a use statement. Using the std::io library provides you with a number of useful features, including the ability to accept user input.
正如你在第 1 章中所看到的,main 函数是程序的入口点:
As you saw in Chapter 1, the main function is the entry point into the program:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:main}}
fn 语法声明了一个新函数;圆括号 () 表示没有参数;而花括号 { 开始了函数体。
The fn syntax declares a new function; the parentheses, (), indicate there are no parameters; and the curly bracket, {, starts the body of the function.
正如你还在第 1 章中学到的,println! 是一个将字符串打印到屏幕上的宏:
As you also learned in Chapter 1, println! is a macro that prints a string to the screen:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print}}
这段代码正在打印一个提示,说明游戏内容并请求用户输入。
This code is printing a prompt stating what the game is and requesting input from the user.
使用变量存储值 (Storing Values with Variables)
Storing Values with Variables
接下来,我们将创建一个 变量 (variable) 来存储用户输入,如下所示:
Next, we’ll create a variable to store the user input, like this:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:string}}
现在程序变得有趣了!在这一小行中发生了很多事情。我们使用 let 语句来创建变量。这里有另一个例子:
Now the program is getting interesting! There’s a lot going on in this little line. We use the let statement to create the variable. Here’s another example:
let apples = 5;
这一行创建了一个名为 apples 的新变量,并将它绑定到值 5。在 Rust 中,变量默认是不可变的 (immutable),这意味着一旦我们给变量一个值,这个值就不会改变。我们将在第 3 章的 “变量与可变性” 部分详细讨论这个概念。要使变量可变 (mutable),我们在变量名之前添加 mut:
This line creates a new variable named apples and binds it to the value 5. In Rust, variables are immutable by default, meaning once we give the variable a value, the value won’t change. We’ll be discussing this concept in detail in the “Variables and Mutability” section in Chapter 3. To make a variable mutable, we add mut before the variable name:
let apples = 5; // 不可变 (immutable)
let mut bananas = 5; // 可变 (mutable)
注意:
//语法开始一个注释,该注释一直持续到行尾。Rust 会忽略注释中的所有内容。我们将在 第 3 章 中更详细地讨论注释。
Note: The
//syntax starts a comment that continues until the end of the line. Rust ignores everything in comments. We’ll discuss comments in more detail in Chapter 3.
回到猜数字游戏程序,你现在知道 let mut guess 将引入一个名为 guess 的可变变量。等号 (=) 告诉 Rust 我们现在想要将某些东西绑定到该变量。等号右边是 guess 所绑定的值,它是调用 String::new 的结果,该函数返回 String 的一个新实例。String 是标准库提供的一种字符串类型,它是一种可增长的、UTF-8 编码的文本片段。
Returning to the guessing game program, you now know that let mut guess will introduce a mutable variable named guess. The equal sign (=) tells Rust we want to bind something to the variable now. On the right of the equal sign is the value that guess is bound to, which is the result of calling String::new, a function that returns a new instance of a String. String is a string type provided by the standard library that is a growable, UTF-8 encoded bit of text.
::new 行中的 :: 语法表示 new 是 String 类型的一个关联函数 (associated function)。关联函数 是在某种类型上实现的函数,在本例中是 String。这个 new 函数创建了一个新的空字符串。你会在许多类型上发现 new 函数,因为它是创建某种新值的函数的常用名称。
The :: syntax in the ::new line indicates that new is an associated function of the String type. An associated function is a function that’s implemented on a type, in this case String. This new function creates a new, empty string. You’ll find a new function on many types because it’s a common name for a function that makes a new value of some kind.
总而言之,let mut guess = String::new(); 这一行创建了一个可变变量,该变量目前绑定到一个新的空 String 实例。呼!
In full, the let mut guess = String::new(); line has created a mutable variable that is currently bound to a new, empty instance of a String. Whew!
接收用户输入 (Receiving User Input)
Receiving User Input
回想一下,我们在程序的第一行使用 use std::io; 引入了标准库的输入/输出功能。现在我们将调用 io 模块中的 stdin 函数,这将允许我们处理用户输入:
Recall that we included the input/output functionality from the standard library with use std::io; on the first line of the program. Now we’ll call the stdin function from the io module, which will allow us to handle user input:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:read}}
如果我们没有在程序开头使用 use std::io; 导入 io 模块,我们仍然可以通过将此函数调用写为 std::io::stdin 来使用该函数。stdin 函数返回 std::io::Stdin 的一个实例,该类型代表终端标准输入的句柄 (handle)。
If we hadn’t imported the io module with use std::io; at the beginning of the program, we could still use the function by writing this function call as std::io::stdin. The stdin function returns an instance of std::io::Stdin, which is a type that represents a handle to the standard input for your terminal.
接下来,.read_line(&mut guess) 行在标准输入句柄上调用 read_line 方法,以获取用户的输入。我们还将 &mut guess 作为参数传递给 read_line,以告诉它将用户输入存储在哪个字符串中。read_line 的全部工作是获取用户在标准输入中输入的任何内容,并将其追加到字符串中(不覆盖其内容),因此我们将该字符串作为参数传递。字符串参数需要是可变的,以便该方法可以更改字符串的内容。
Next, the line .read_line(&mut guess) calls the read_line method on the standard input handle to get input from the user. We’re also passing &mut guess as the argument to read_line to tell it what string to store the user input in. The full job of read_line is to take whatever the user types into standard input and append that into a string (without overwriting its contents), so we therefore pass that string as an argument. The string argument needs to be mutable so that the method can change the string’s content.
& 表示该参数是一个 引用 (reference),它为你提供了一种方法,让代码的多个部分访问同一块数据,而无需多次将该数据复制到内存中。引用是一个复杂的功能,而 Rust 的主要优势之一就是使用引用的安全性和简便性。你不需要了解这些细节就可以完成这个程序。目前,你只需要知道,像变量一样,引用默认也是不可变的。因此,你需要编写 &mut guess 而不是 &guess 来使其可变。(第 4 章将更深入地解释引用。)
The & indicates that this argument is a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times. References are a complex feature, and one of Rust’s major advantages is how safe and easy it is to use references. You don’t need to know a lot of those details to finish this program. For now, all you need to know is that, like variables, references are immutable by default. Hence, you need to write &mut guess rather than &guess to make it mutable. (Chapter 4 will explain references more thoroughly.)
使用 Result 处理潜在的失败 (Handling Potential Failure with Result)
Handling Potential Failure with Result
我们仍在处理这行代码。我们现在正在讨论第三行文本,但请注意,它仍然是单个逻辑代码行的一部分。下一部分是这个方法:
We’re still working on this line of code. We’re now discussing a third line of text, but note that it’s still part of a single logical line of code. The next part is this method:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:expect}}
我们可以将这段代码写成:
We could have written this code as:
io::stdin().read_line(&mut guess).expect("Failed to read line");
然而,过长的行难以阅读,因此最好将其拆分。当你使用 .method_name() 语法调用方法时,引入换行符和其他空白符来帮助分解长行通常是明智之举。现在让我们讨论这一行的作用。
However, one long line is difficult to read, so it’s best to divide it. It’s often wise to introduce a newline and other whitespace to help break up long lines when you call a method with the .method_name() syntax. Now let’s discuss what this line does.
如前所述,read_line 将用户输入的任何内容放入我们传递给它的字符串中,但它也会返回一个 Result 值。Result 是一个 枚举 (enumeration)(通常简称为 enum),它是一种可以处于多种可能状态之一的类型。我们称每个可能的状态为 变体 (variant)。
As mentioned earlier, read_line puts whatever the user enters into the string we pass to it, but it also returns a Result value. Result is an enumeration, often called an enum, which is a type that can be in one of multiple possible states. We call each possible state a variant.
第 6 章 将更详细地讨论枚举。这些 Result 类型的目的是编码错误处理信息。
Chapter 6 will cover enums in more detail. The purpose of these Result types is to encode error-handling information.
Result 的变体是 Ok 和 Err。Ok 变体表示操作成功,并包含成功生成的值。Err 变体表示操作失败,并包含有关操作如何失败或为何失败的信息。
Result’s variants are Ok and Err. The Ok variant indicates the operation was successful, and it contains the successfully generated value. The Err variant means the operation failed, and it contains information about how or why the operation failed.
Result 类型的值,就像任何类型的值一样,在其上定义了方法。Result 的实例有一个你可以调用的 expect 方法。如果这个 Result 实例是一个 Err 值,expect 将导致程序崩溃,并显示你作为参数传递给 expect 的消息。如果 read_line 方法返回 Err,这很可能是底层操作系统产生的错误结果。如果这个 Result 实例是一个 Ok 值,expect 将获取 Ok 持有的返回值,并仅将该值返回给你,以便你可以使用它。在本例中,该值是用户输入中的字节数。
Values of the Result type, like values of any type, have methods defined on them. An instance of Result has an expect method that you can call. If this instance of Result is an Err value, expect will cause the program to crash and display the message that you passed as an argument to expect. If the read_line method returns an Err, it would likely be the result of an error coming from the underlying operating system. If this instance of Result is an Ok value, expect will take the return value that Ok is holding and return just that value to you so that you can use it. In this case, that value is the number of bytes in the user’s input.
如果你不调用 expect,程序可以编译,但你会得到一个警告:
If you don’t call expect, the program will compile, but you’ll get a warning:
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/output.txt}}
Rust 警告你没有使用 read_line 返回的 Result 值,这表明程序尚未处理可能出现的错误。
Rust warns that you haven’t used the Result value returned from read_line, indicating that the program hasn’t handled a possible error.
消除警告的正确方法是实际编写错误处理代码,但在我们的案例中,我们只想在出现问题时让程序崩溃,因此我们可以使用 expect。你将在 第 9 章 中学习如何从错误中恢复。
The right way to suppress the warning is to actually write error-handling code, but in our case we just want to crash this program when a problem occurs, so we can use expect. You’ll learn about recovering from errors in Chapter 9.
使用 println! 占位符打印值 (Printing Values with println! Placeholders)
Printing Values with println! Placeholders
除了结尾的花括号外,到目前为止的代码中只有一行需要讨论:
Aside from the closing curly bracket, there’s only one more line to discuss in the code so far:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print_guess}}
这一行打印现在包含用户输入的字符串。{} 这对花括号是一个占位符:把 {} 想象成固定值的小螃蟹钳。在打印变量的值时,变量名可以放在花括号内。在打印评估表达式的结果时,在格式字符串中放置空花括号,然后在格式字符串后跟一个逗号分隔的表达式列表,以按相同顺序打印在每个空花括号占位符中。在一次调用 println! 中同时打印变量和表达式的结果看起来像这样:
This line prints the string that now contains the user’s input. The {} set of curly brackets is a placeholder: Think of {} as little crab pincers that hold a value in place. When printing the value of a variable, the variable name can go inside the curly brackets. When printing the result of evaluating an expression, place empty curly brackets in the format string, then follow the format string with a comma-separated list of expressions to print in each empty curly bracket placeholder in the same order. Printing a variable and the result of an expression in one call to println! would look like this:
#![allow(unused)]
fn main() {
let x = 5;
let y = 10;
println!("x = {x} and y + 2 = {}", y + 2);
}
这段代码将打印 x = 5 and y + 2 = 12。
This code would print x = 5 and y + 2 = 12.
测试第一部分 (Testing the First Part)
Testing the First Part
让我们测试猜数字游戏的第一部分。使用 cargo run 运行它:
Let’s test the first part of the guessing game. Run it using cargo run:
$ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.44s
Running `target/debug/guessing_game`
Guess the number!
Please input your guess.
6
You guessed: 6
此时,游戏的第一部分已经完成:我们从键盘获取输入,然后将其打印出来。
At this point, the first part of the game is done: We’re getting input from the keyboard and then printing it.
生成一个秘密数字 (Generating a Secret Number)
Generating a Secret Number
接下来,我们需要生成一个秘密数字供用户尝试猜测。秘密数字每次都应该不同,这样游戏玩起来才会有趣。我们将使用 1 到 100 之间的随机数,这样游戏就不会太难。Rust 尚未在其标准库中包含随机数功能。但是,Rust 团队确实提供了一个具有该功能的 rand crate。
Next, we need to generate a secret number that the user will try to guess. The secret number should be different every time so that the game is fun to play more than once. We’ll use a random number between 1 and 100 so that the game isn’t too difficult. Rust doesn’t yet include random number functionality in its standard library. However, the Rust team does provide a rand crate with said functionality.
通过 crate 增加功能 (Increasing Functionality with a Crate)
Increasing Functionality with a Crate
请记住,crate 是 Rust 源代码文件的集合。我们一直在构建的项目是一个二进制 crate (binary crate),它是一个可执行文件。rand crate 是一个库 crate (library crate),其中包含旨在用于其他程序的代码,且不能独立执行。
Remember that a crate is a collection of Rust source code files. The project we’ve been building is a binary crate, which is an executable. The rand crate is a library crate, which contains code that is intended to be used in other programs and can’t be executed on its own.
Cargo 对外部 crate 的协调是 Cargo 真正大放异彩的地方。在我们编写使用 rand 的代码之前,我们需要修改 Cargo.toml 文件,将 rand crate 作为依赖项包含在内。现在打开该文件,并在 Cargo 为你创建的 [dependencies] 节标题下方添加以下行。请务必严格按照我们这里的格式指定 rand,并使用此版本号,否则本教程中的代码示例可能无法运行:
Cargo’s coordination of external crates is where Cargo really shines. Before we can write code that uses rand, we need to modify the Cargo.toml file to include the rand crate as a dependency. Open that file now and add the following line to the bottom, beneath the [dependencies] section header that Cargo created for you. Be sure to specify rand exactly as we have here, with this version number, or the code examples in this tutorial may not work:
文件名:Cargo.toml (Filename: Cargo.toml)
{{#include ../listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml:8:}}
在 Cargo.toml 文件中,标题后面的所有内容都是该节的一部分,该节一直持续到另一个节开始。在 [dependencies] 中,你告诉 Cargo 你的项目依赖于哪些外部 crate 以及你需要的这些 crate 的版本。在这种情况下,我们使用语义化版本说明符 0.8.5 指定 rand crate。Cargo 理解 语义化版本 (Semantic Versioning)(有时称为 SemVer),这是一种编写版本号的标准。说明符 0.8.5 实际上是 ^0.8.5 的简写,这意味着任何至少为 0.8.5 但低于 0.9.0 的版本。
In the Cargo.toml file, everything that follows a header is part of that section that continues until another section starts. In [dependencies], you tell Cargo which external crates your project depends on and which versions of those crates you require. In this case, we specify the rand crate with the semantic version specifier 0.8.5. Cargo understands Semantic Versioning (sometimes called SemVer), which is a standard for writing version numbers. The specifier 0.8.5 is actually shorthand for ^0.8.5, which means any version that is at least 0.8.5 but below 0.9.0.
Cargo 认为这些版本的公共 API 与版本 0.8.5 兼容,此规范可确保你将获得最新的修订版,且该版本仍能与本章中的代码一起编译。任何 0.9.0 或更高版本都不保证具有与以下示例相同的 API。
Cargo considers these versions to have public APIs compatible with version 0.8.5, and this specification ensures that you’ll get the latest patch release that will still compile with the code in this chapter. Any version 0.9.0 or greater is not guaranteed to have the same API as what the following examples use.
现在,在不更改任何代码的情况下,让我们构建项目,如示例 2-2 所示。
Now, without changing any of the code, let’s build the project, as shown in Listing 2-2.
$ cargo build
Updating crates.io index
Locking 15 packages to latest Rust 1.85.0 compatible versions
Adding rand v0.8.5 (available: v0.9.0)
Compiling proc-macro2 v1.0.93
Compiling unicode-ident v1.0.17
Compiling libc v0.2.170
Compiling cfg-if v1.0.0
Compiling byteorder v1.5.0
Compiling getrandom v0.2.15
Compiling rand_core v0.6.4
Compiling quote v1.0.38
Compiling syn v2.0.98
Compiling zerocopy-derive v0.7.35
Compiling zerocopy v0.7.35
Compiling ppv-lite86 v0.2.20
Compiling rand_chacha v0.3.1
Compiling rand v0.8.5
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.48s
你可能会看到不同的版本号(但由于 SemVer,它们都会与代码兼容!)和不同的行(取决于操作系统),并且这些行的顺序可能不同。
You may see different version numbers (but they will all be compatible with the code, thanks to SemVer!) and different lines (depending on the operating system), and the lines may be in a different order.
当我们包含外部依赖项时,Cargo 会从 注册表 (registry) 中获取该依赖项所需的所有最新版本,注册表是来自 Crates.io 的数据副本。Crates.io 是 Rust 生态系统中的人们发布其开源 Rust 项目供他人使用的地方。
When we include an external dependency, Cargo fetches the latest versions of everything that dependency needs from the registry, which is a copy of data from Crates.io. Crates.io is where people in the Rust ecosystem post their open source Rust projects for others to use.
更新注册表后,Cargo 会检查 [dependencies] 部分,并下载任何列出的但尚未下载的 crate。在本例中,虽然我们只将 rand 列为依赖项,但 Cargo 也会抓取 rand 正常运行所依赖的其他 crate。下载完这些 crate 后,Rust 会编译它们,然后在依赖项可用的情况下编译项目。
After updating the registry, Cargo checks the [dependencies] section and downloads any crates listed that aren’t already downloaded. In this case, although we only listed rand as a dependency, Cargo also grabbed other crates that rand depends on to work. After downloading the crates, Rust compiles them and then compiles the project with the dependencies available.
如果你在不做任何更改的情况下立即再次运行 cargo build,除了 Finished 行之外,你不会得到任何输出。Cargo 知道它已经下载并编译了依赖项,并且你没有在 Cargo.toml 文件中更改关于它们的任何内容。Cargo 还知道你没有更改关于代码的任何内容,因此它也不会重新编译代码。由于无事可做,它直接退出。
If you immediately run cargo build again without making any changes, you won’t get any output aside from the Finished line. Cargo knows it has already downloaded and compiled the dependencies, and you haven’t changed anything about them in your Cargo.toml file. Cargo also knows that you haven’t changed anything about your code, so it doesn’t recompile that either. With nothing to do, it simply exits.
如果你打开 src/main.rs 文件,做一些微不足道的更改,然后保存并再次构建,你将只能看到两行输出:
If you open the src/main.rs file, make a trivial change, and then save it and build again, you’ll only see two lines of output:
$ cargo build
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
这些行显示 Cargo 仅根据你对 src/main.rs 文件的微小更改更新了构建。你的依赖项没有改变,因此 Cargo 知道它可以重用已经为这些依赖项下载和编译的内容。
These lines show that Cargo only updates the build with your tiny change to the src/main.rs file. Your dependencies haven’t changed, so Cargo knows it can reuse what it has already downloaded and compiled for those.
确保可重复构建 (Ensuring Reproducible Builds)
Ensuring Reproducible Builds
Cargo 有一种机制可以确保你或任何其他人在构建你的代码时,每次都能重建相同的产物:Cargo 将仅使用你指定的依赖项版本,除非你另有指示。例如,假设下周 rand crate 的 0.8.6 版本发布了,该版本包含一个重要的错误修复,但也包含一个会导致你的代码损坏的回归。为了处理这种情况,Rust 在你第一次运行 cargo build 时创建了 Cargo.lock 文件,因此我们现在在 guessing_game 目录中有了这个文件。
Cargo has a mechanism that ensures that you can rebuild the same artifact every time you or anyone else builds your code: Cargo will use only the versions of the dependencies you specified until you indicate otherwise. For example, say that next week version 0.8.6 of the rand crate comes out, and that version contains an important bug fix, but it also contains a regression that will break your code. To handle this, Rust creates the Cargo.lock file the first time you run cargo build, so we now have this in the guessing_game directory.
当你第一次构建项目时,Cargo 会找出所有符合标准的依赖项版本,然后将它们写入 Cargo.lock 文件。当你将来构建项目时,Cargo 会发现 Cargo.lock 文件存在,并使用其中指定的版本,而不是再次执行所有找出版本的工作。这使你能够自动拥有可重复的构建。换句话说,由于 Cargo.lock 文件的存在,你的项目将保持在 0.8.5,直到你显式升级。由于 Cargo.lock 文件对于可重复构建非常重要,因此它通常与项目中的其余代码一起提交到源代码控制系统中。
When you build a project for the first time, Cargo figures out all the versions of the dependencies that fit the criteria and then writes them to the Cargo.lock file. When you build your project in the future, Cargo will see that the Cargo.lock file exists and will use the versions specified there rather than doing all the work of figuring out versions again. This lets you have a reproducible build automatically. In other words, your project will remain at 0.8.5 until you explicitly upgrade, thanks to the Cargo.lock file. Because the Cargo.lock file is important for reproducible builds, it’s often checked into source control with the rest of the code in your project.
更新 crate 以获取新版本 (Updating a Crate to Get a New Version)
Updating a Crate to Get a New Version
当你 确实 想更新 crate 时,Cargo 提供了 update 命令,它将忽略 Cargo.lock 文件,并找出符合你在 Cargo.toml 中指定的所有最新版本。然后,Cargo 会将这些版本写入 Cargo.lock 文件。否则,默认情况下,Cargo 仅会查找大于 0.8.5 且小于 0.9.0 的版本。如果 rand crate 发布了两个新版本 0.8.6 和 0.999.0,如果你运行 cargo update,你将看到以下内容:
When you do want to update a crate, Cargo provides the command update, which will ignore the Cargo.lock file and figure out all the latest versions that fit your specifications in Cargo.toml. Cargo will then write those versions to the Cargo.lock file. Otherwise, by default, Cargo will only look for versions greater than 0.8.5 and less than 0.9.0. If the rand crate has released the two new versions 0.8.6 and 0.999.0, you would see the following if you ran cargo update:
$ cargo update
Updating crates.io index
Locking 1 package to latest Rust 1.85.0 compatible version
Updating rand v0.8.5 -> v0.8.6 (available: v0.999.0)
Cargo 忽略了 0.999.0 版本。此时,你还会注意到 Cargo.lock 文件发生了变化,指出你现在使用的 rand crate 版本是 0.8.6。要使用 rand 版本 0.999.0 或 0.999.x 系列中的任何版本,你必须更新 Cargo.toml 文件,使其看起来像这样(实际上不要做此更改,因为以下示例假设你使用的是 rand 0.8):
Cargo ignores the 0.999.0 release. At this point, you would also notice a change in your Cargo.lock file noting that the version of the rand crate you are now using is 0.8.6. To use rand version 0.999.0 or any version in the 0.999.x series, you’d have to update the Cargo.toml file to look like this instead (don’t actually make this change because the following examples assume you’re using rand 0.8):
[dependencies]
rand = "0.999.0"
下一次运行 cargo build 时,Cargo 将更新可用 crate 的注册表,并根据你指定的新版本重新评估你的 rand 需求。
The next time you run cargo build, Cargo will update the registry of crates available and reevaluate your rand requirements according to the new version you have specified.
关于 Cargo 及其 生态系统 还有很多内容要讲,我们将在第 14 章中讨论,但现在,这就是你需要知道的全部内容。Cargo 使得重用库变得非常容易,因此 Rustaceans 能够编写由许多包组装而成的小型项目。
There’s a lot more to say about Cargo and its ecosystem, which we’ll discuss in Chapter 14, but for now, that’s all you need to know. Cargo makes it very easy to reuse libraries, so Rustaceans are able to write smaller projects that are assembled from a number of packages.
生成一个随机数 (Generating a Random Number)
Generating a Random Number
让我们开始使用 rand 来生成一个要猜测的数字。下一步是更新 src/main.rs,如示例 2-3 所示。
Let’s start using rand to generate a number to guess. The next step is to update src/main.rs, as shown in Listing 2-3.
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs:all}}
首先,我们添加 use rand::Rng; 这一行。Rng trait 定义了随机数生成器实现的方法,并且这个 trait 必须在作用域内,我们才能使用这些方法。第 10 章将详细介绍 trait。
First, we add the line use rand::Rng;. The Rng trait defines methods that random number generators implement, and this trait must be in scope for us to use those methods. Chapter 10 will cover traits in detail.
接下来,我们在中间添加两行。在第一行中,我们调用 rand::thread_rng 函数,它为我们提供了我们将要使用的特定随机数生成器:一个位于当前执行线程本地并由操作系统设定种子的生成器。然后,我们在随机数生成器上调用 gen_range 方法。此方法由我们使用 use rand::Rng; 语句引入作用域的 Rng trait 定义。gen_range 方法采用一个范围表达式作为参数,并生成该范围内的随机数。我们在这里使用的这种范围表达式采用 start..=end 的形式,并且包含下限和上限,因此我们需要指定 1..=100 来请求 1 到 100 之间的数字。
Next, we’re adding two lines in the middle. In the first line, we call the rand::thread_rng function that gives us the particular random number generator we’re going to use: one that is local to the current thread of execution and is seeded by the operating system. Then, we call the gen_range method on the random number generator. This method is defined by the Rng trait that we brought into scope with the use rand::Rng; statement. The gen_range method takes a range expression as an argument and generates a random number in the range. The kind of range expression we’re using here takes the form start..=end and is inclusive on the lower and upper bounds, so we need to specify 1..=100 to request a number between 1 and 100.
注意:你不会仅凭直觉就知道要从一个 crate 中使用哪些 trait 以及调用哪些方法和函数,因此每个 crate 都有包含其使用说明的文档。Cargo 的另一个巧妙功能是,运行
cargo doc --open命令将在本地构建所有依赖项提供的文档,并在浏览器中打开。例如,如果你对randcrate 中的其他功能感兴趣,请运行cargo doc --open并在左侧边栏中点击rand。
Note: You won’t just know which traits to use and which methods and functions to call from a crate, so each crate has documentation with instructions for using it. Another neat feature of Cargo is that running the
cargo doc --opencommand will build documentation provided by all your dependencies locally and open it in your browser. If you’re interested in other functionality in therandcrate, for example, runcargo doc --openand clickrandin the sidebar on the left.
第二个新行打印秘密数字。这在开发程序以便能够对其进行测试时非常有用,但我们将在最终版本中删除它。如果程序一启动就打印答案,那就不成其为游戏了!
The second new line prints the secret number. This is useful while we’re developing the program to be able to test it, but we’ll delete it from the final version. It’s not much of a game if the program prints the answer as soon as it starts!
尝试运行该程序几次:
Try running the program a few times:
$ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/guessing_game`
Guess the number!
The secret number is: 7
Please input your guess.
4
You guessed: 4
$ cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/guessing_game`
Guess the number!
The secret number is: 83
Please input your guess.
5
You guessed: 5
你应该得到不同的随机数,并且它们都应该是 1 到 100 之间的数字。干得好!
You should get different random numbers, and they should all be numbers between 1 and 100. Great job!
比较猜测数字与秘密数字 (Comparing the Guess to the Secret Number)
Comparing the Guess to the Secret Number
现在我们有了用户输入和随机数,我们可以对它们进行比较。这一步如示例 2-4 所示。请注意,正如我们将要解释的,这段代码暂时还不能编译。
Now that we have user input and a random number, we can compare them. That step is shown in Listing 2-4. Note that this code won’t compile just yet, as we will explain.
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs:here}}
首先,我们添加另一个 use 语句,从标准库中将一个名为 std::cmp::Ordering 的类型引入作用域。Ordering 类型是另一个枚举,其变体为 Less、Greater 和 Equal。当你比较两个值时,这是可能出现的三种结果。
First, we add another use statement, bringing a type called std::cmp::Ordering into scope from the standard library. The Ordering type is another enum and has the variants Less, Greater, and Equal. These are the three outcomes that are possible when you compare two values.
然后,我们在底部添加了五行使用 Ordering 类型的新代码。cmp 方法比较两个值,并且可以被任何可以进行比较的对象调用。它采用指向你要比较的任何内容的引用:在这里,它正在将 guess 与 secret_number 进行比较。然后,它返回我们在 use 语句中引入作用域的 Ordering 枚举的一个变体。我们使用 match 表达式,根据调用 cmp 比较 guess 和 secret_number 的值后返回的 Ordering 变体来决定下一步该做什么。
Then, we add five new lines at the bottom that use the Ordering type. The cmp method compares two values and can be called on anything that can be compared. It takes a reference to whatever you want to compare with: Here, it’s comparing guess to secret_number. Then, it returns a variant of the Ordering enum we brought into scope with the use statement. We use a match expression to decide what to do next based on which variant of Ordering was returned from the call to cmp with the values in guess and secret_number.
match 表达式由 分支 (arm) 组成。一个分支包含一个用于匹配的 模式 (pattern),以及如果提供给 match 的值符合该分支模式时应运行的代码。Rust 获取提供给 match 的值,并依次检查每个分支的模式。模式和 match 结构是强大的 Rust 功能:它们让你能够表达代码可能遇到的各种情况,并确保你处理了所有情况。这些功能将分别在第 6 章和第 19 章中详细介绍。
A match expression is made up of arms. An arm consists of a pattern to match against, and the code that should be run if the value given to match fits that arm’s pattern. Rust takes the value given to match and looks through each arm’s pattern in turn. Patterns and the match construct are powerful Rust features: They let you express a variety of situations your code might encounter, and they make sure you handle them all. These features will be covered in detail in Chapter 6 and Chapter 19, respectively.
让我们用这里使用的 match 表达式走一遍例子。假设用户猜测了 50,而这次随机生成的秘密数字是 38。
Let’s walk through an example with the match expression we use here. Say that the user has guessed 50 and the randomly generated secret number this time is 38.
当代码将 50 与 38 进行比较时,cmp 方法将返回 Ordering::Greater,因为 50 大于 38。match 表达式获取 Ordering::Greater 值,并开始检查每个分支的模式。它查看第一个分支的模式 Ordering::Less,发现值 Ordering::Greater 与 Ordering::Less 不匹配,因此它忽略该分支中的代码并移至下一个分支。下一个分支的模式是 Ordering::Greater,它 确实 与 Ordering::Greater 匹配!该分支中的相关代码将执行,并在屏幕上打印 Too big!。match 表达式在第一次成功匹配后结束,因此在这种情况下它不会查看最后一个分支。
When the code compares 50 to 38, the cmp method will return Ordering::Greater because 50 is greater than 38. The match expression gets the Ordering::Greater value and starts checking each arm’s pattern. It looks at the first arm’s pattern, Ordering::Less, and sees that the value Ordering::Greater does not match Ordering::Less, so it ignores the code in that arm and moves to the next arm. The next arm’s pattern is Ordering::Greater, which does match Ordering::Greater! The associated code in that arm will execute and print Too big! to the screen. The match expression ends after the first successful match, so it won’t look at the last arm in this scenario.
但是,示例 2-4 中的代码暂时还无法编译。让我们试一下:
However, the code in Listing 2-4 won’t compile yet. Let’s try it:
{{#include ../listings/ch02-guessing-game-tutorial/listing-02-04/output.txt}}
错误的核心指出存在 类型不匹配 (mismatched types)。Rust 具有强大的静态类型系统。但是,它也有类型推断 (type inference)。当我们编写 let mut guess = String::new() 时,Rust 能够推断出 guess 应该是 String 类型,并且没有让我们写出类型。另一方面,secret_number 是一种数字类型。一些 Rust 的数字类型可以具有 1 到 100 之间的值:i32,一种 32 位数字;u32,一种无符号 32 位数字;i64,一种 64 位数字;以及其他类型。除非另有说明,Rust 默认使用 i32,这就是 secret_number 的类型,除非你在其他地方添加了会导致 Rust 推断出不同数值类型的类型信息。产生错误的原因是 Rust 无法比较字符串和数字类型。
The core of the error states that there are mismatched types. Rust has a strong, static type system. However, it also has type inference. When we wrote let mut guess = String::new(), Rust was able to infer that guess should be a String and didn’t make us write the type. The secret_number, on the other hand, is a number type. A few of Rust’s number types can have a value between 1 and 100: i32, a 32-bit number; u32, an unsigned 32-bit number; i64, a 64-bit number; as well as others. Unless otherwise specified, Rust defaults to an i32, which is the type of secret_number unless you add type information elsewhere that would cause Rust to infer a different numerical type. The reason for the error is that Rust cannot compare a string and a number type.
最终,我们希望将程序读取为输入的 String 转换为数字类型,以便我们可以将其与秘密数字进行数值比较。我们通过在 main 函数体中添加这一行来实现:
Ultimately, we want to convert the String the program reads as input into a number type so that we can compare it numerically to the secret number. We do so by adding this line to the main function body:
文件名:src/main.rs (Filename: src/main.rs)
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs:here}}
这一行是:
The line is:
let guess: u32 = guess.trim().parse().expect("Please type a number!");
我们创建了一个名为 guess 的变量。但等等,程序不是已经有一个名为 guess 的变量了吗?确实有,但幸好 Rust 允许我们用一个新值来重影 (shadow) 之前的 guess 值。重影 (Shadowing) 允许我们重用 guess 变量名,而不是强迫我们创建两个唯一的变量,例如 guess_str 和 guess。我们将在 第 3 章 中更详细地讨论这一点,但现在,请记住,当你想要将一个值从一种类型转换为另一种类型时,经常会使用此功能。
We create a variable named guess. But wait, doesn’t the program already have a variable named guess? It does, but helpfully Rust allows us to shadow the previous value of guess with a new one. Shadowing lets us reuse the guess variable name rather than forcing us to create two unique variables, such as guess_str and guess, for example. We’ll cover this in more detail in Chapter 3, but for now, know that this feature is often used when you want to convert a value from one type to another type.
我们将这个新变量绑定到表达式 guess.trim().parse()。表达式中的 guess 指的是包含字符串输入的原始 guess 变量。String 实例上的 trim 方法将消除开头和结尾的任何空白,在我们将字符串转换为 u32 之前必须这样做,因为 u32 只能包含数字数据。用户必须按下 enter 键才能满足 read_line 并输入他们的猜测,这会向字符串中添加一个换行符。例如,如果用户输入 5 并按下 enter,guess 看起来像这样:5\n。\n 代表 “换行符”。(在 Windows 上,按下 enter 键会产生回车符和换行符 \r\n。)trim 方法会消除 \n 或 \r\n,结果只剩下 5。
We bind this new variable to the expression guess.trim().parse(). The guess in the expression refers to the original guess variable that contained the input as a string. The trim method on a String instance will eliminate any whitespace at the beginning and end, which we must do before we can convert the string to a u32, which can only contain numerical data. The user must press enter to satisfy read_line and input their guess, which adds a newline character to the string. For example, if the user types 5 and presses enter, guess looks like this: 5\n. The \n represents “newline.” (On Windows, pressing enter results in a carriage return and a newline, \r\n.) The trim method eliminates \n or \r\n, resulting in just 5.
字符串上的 parse 方法 将字符串转换为另一种类型。在这里,我们使用它将字符串转换为数字。我们需要通过使用 let guess: u32 来告诉 Rust 我们需要的确切数字类型。guess 之后的冒号 (:) 告诉 Rust 我们将标注变量的类型。Rust 有几种内置的数字类型;这里看到的 u32 是一个无符号 32 位整数。它是小型正数的良好默认选择。你将在 第 3 章 中了解其他数字类型。
The parse method on strings converts a string to another type. Here, we use it to convert from a string to a number. We need to tell Rust the exact number type we want by using let guess: u32. The colon (:) after guess tells Rust we’ll annotate the variable’s type. Rust has a few built-in number types; the u32 seen here is an unsigned, 32-bit integer. It’s a good default choice for a small positive number. You’ll learn about other number types in Chapter 3.
此外,此示例程序中的 u32 标注以及与 secret_number 的比较意味着 Rust 也会推断出 secret_number 也应该是 u32。所以,现在比较将在两个相同类型的值之间进行!
Additionally, the u32 annotation in this example program and the comparison with secret_number means Rust will infer that secret_number should be a u32 as well. So, now the comparison will be between two values of the same type!
parse 方法仅对逻辑上可以转换为数字的字符起作用,因此很容易导致错误。例如,如果字符串包含 A👍%,则无法将其转换为数字。因为它可能会失败,所以 parse 方法返回一个 Result 类型,就像 read_line 方法一样(前面在 “使用 Result 处理潜在的失败” 中讨论过)。我们将通过再次使用 expect 方法以相同的方式处理此 Result。如果 parse 因为无法从字符串创建数字而返回 Err Result 变体,则 expect 调用将使游戏崩溃并打印我们给它的消息。如果 parse 能够成功地将字符串转换为数字,它将返回 Result 的 Ok 变体,而 expect 将从 Ok 值中返回我们想要的数字。
The parse method will only work on characters that can logically be converted into numbers and so can easily cause errors. If, for example, the string contained A👍%, there would be no way to convert that to a number. Because it might fail, the parse method returns a Result type, much as the read_line method does (discussed earlier in “Handling Potential Failure with Result”). We’ll treat this Result the same way by using the expect method again. If parse returns an Err Result variant because it couldn’t create a number from the string, the expect call will crash the game and print the message we give it. If parse can successfully convert the string to a number, it will return the Ok variant of Result, and expect will return the number that we want from the Ok value.
现在让我们运行程序:
Let’s run the program now:
$ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.26s
Running `target/debug/guessing_game`
Guess the number!
The secret number is: 58
Please input your guess.
76
You guessed: 76
Too big!
不错!即使在猜测数字前添加了空格,程序仍然算出用户猜的是 76。运行程序几次,以验证不同种类输入的行为:正确猜中数字、猜的数字太高以及猜的数字太低。
Nice! Even though spaces were added before the guess, the program still figured out that the user guessed 76. Run the program a few times to verify the different behavior with different kinds of input: Guess the number correctly, guess a number that is too high, and guess a number that is too low.
现在我们已经完成了大部分游戏的运行,但是用户只能进行一次猜测。让我们通过添加循环来改变这一点!
We have most of the game working now, but the user can make only one guess. Let’s change that by adding a loop!
通过循环允许多次猜测 (Allowing Multiple Guesses with Looping)
Allowing Multiple Guesses with Looping
loop 关键字创建了一个无限循环。我们将添加一个循环,让用户有更多机会猜测数字:
The loop keyword creates an infinite loop. We’ll add a loop to give users more chances at guessing the number:
文件名:src/main.rs (Filename: src/main.rs)
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs:here}}
如你所见,我们将从猜测输入提示开始的所有内容都移到了循环中。确保将循环内的各行再缩进四个空格,并再次运行程序。程序现在将永远请求另一个猜测,这实际上引入了一个新问题。看起来用户无法退出了!
As you can see, we’ve moved everything from the guess input prompt onward into a loop. Be sure to indent the lines inside the loop another four spaces each and run the program again. The program will now ask for another guess forever, which actually introduces a new problem. It doesn’t seem like the user can quit!
用户始终可以使用键盘快捷键 ctrl-C 中断程序。但还有另一种方法可以逃离这个贪得无厌的怪物,正如在 “比较猜测数字与秘密数字” 的 parse 讨论中提到的:如果用户输入非数字答案,程序将崩溃。我们可以利用这一点来允许用户退出,如下所示:
The user could always interrupt the program by using the keyboard shortcut ctrl-C. But there’s another way to escape this insatiable monster, as mentioned in the parse discussion in “Comparing the Guess to the Secret Number”: If the user enters a non-number answer, the program will crash. We can take advantage of that to allow the user to quit, as shown here:
$ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
Running `target/debug/guessing_game`
Guess the number!
The secret number is: 59
Please input your guess.
45
You guessed: 45
Too small!
Please input your guess.
60
You guessed: 60
Too big!
Please input your guess.
59
You guessed: 59
You win!
Please input your guess.
quit
thread 'main' panicked at src/main.rs:28:47:
Please type a number!: ParseIntError { kind: InvalidDigit }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
输入 quit 将退出游戏,但正如你将注意到的,输入任何其他非数字输入也会退出。这充其量只是次优选择;我们希望游戏在猜中正确数字时也停止。
Typing quit will quit the game, but as you’ll notice, so will entering any other non-number input. This is suboptimal, to say the least; we want the game to also stop when the correct number is guessed.
猜对后退出 (Quitting After a Correct Guess)
Quitting After a Correct Guess
让我们通过添加 break 语句,将游戏编写为在用户获胜时退出:
Let’s program the game to quit when the user wins by adding a break statement:
文件名:src/main.rs (Filename: src/main.rs)
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs:here}}
在 You win! 之后添加 break 行,使程序在用户正确猜中秘密数字时退出循环。退出循环也意味着退出程序,因为循环是 main 的最后一部分。
Adding the break line after You win! makes the program exit the loop when the user guesses the secret number correctly. Exiting the loop also means exiting the program, because the loop is the last part of main.
处理无效输入 (Handling Invalid Input)
Handling Invalid Input
为了进一步完善游戏的运行方式,与其在用户输入非数字时使程序崩溃,不如让游戏忽略非数字,以便用户可以继续猜测。我们可以通过更改将 guess 从 String 转换为 u32 的那一行来实现,如示例 2-5 所示。
To further refine the game’s behavior, rather than crashing the program when the user inputs a non-number, let’s make the game ignore a non-number so that the user can continue guessing. We can do that by altering the line where guess is converted from a String to a u32, as shown in Listing 2-5.
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs:here}}
我们将 expect 调用切换为 match 表达式,以便从错误崩溃转向错误处理。请记住,parse 返回一个 Result 类型,而 Result 是一个包含 Ok 和 Err 变体的枚举。我们在这里使用的是 match 表达式,就像处理 cmp 方法的 Ordering 结果一样。
We switch from an expect call to a match expression to move from crashing on an error to handling the error. Remember that parse returns a Result type and Result is an enum that has the variants Ok and Err. We’re using a match expression here, as we did with the Ordering result of the cmp method.
如果 parse 能够成功地将字符串转换为数字,它将返回一个包含生成的数字的 Ok 值。该 Ok 值将匹配第一个分支的模式,并且 match 表达式将仅返回 parse 产生并放入 Ok 值中的 num 值。该数字最终将出现在我们正在创建的新 guess 变量中,正是我们想要它的地方。
If parse is able to successfully turn the string into a number, it will return an Ok value that contains the resultant number. That Ok value will match the first arm’s pattern, and the match expression will just return the num value that parse produced and put inside the Ok value. That number will end up right where we want it in the new guess variable we’re creating.
如果 parse 不能 将字符串转换为数字,它将返回一个包含更多错误信息的 Err 值。Err 值不匹配第一个 match 分支中的 Ok(num) 模式,但它确实匹配第二个分支中的 Err(_) 模式。下划线 _ 是一个全匹配值;在这个例子中,我们要说的是我们想要匹配所有的 Err 值,不管里面有什么信息。因此,程序将执行第二个分支的代码 continue,它告诉程序进入 loop 的下一次迭代并请求另一个猜测。因此,实际上程序忽略了 parse 可能遇到的所有错误!
If parse is not able to turn the string into a number, it will return an Err value that contains more information about the error. The Err value does not match the Ok(num) pattern in the first match arm, but it does match the Err(_) pattern in the second arm. The underscore, _, is a catch-all value; in this example, we’re saying we want to match all Err values, no matter what information they have inside them. So, the program will execute the second arm’s code, continue, which tells the program to go to the next iteration of the loop and ask for another guess. So, effectively, the program ignores all errors that parse might encounter!
现在程序中的一切都应该按预期工作了。让我们试一下:
Now everything in the program should work as expected. Let’s try it:
$ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
Running `target/debug/guessing_game`
Guess the number!
The secret number is: 61
Please input your guess.
10
You guessed: 10
Too small!
Please input your guess.
99
You guessed: 99
Too big!
Please input your guess.
foo
Please input your guess.
61
You guessed: 61
You win!
太棒了!通过最后一点微调,我们将完成猜数字游戏。回想一下,程序仍在打印秘密数字。这对于测试非常有用,但它破坏了游戏。让我们删除输出秘密数字的 println!。示例 2-6 显示了最终代码。
Awesome! With one tiny final tweak, we will finish the guessing game. Recall that the program is still printing the secret number. That worked well for testing, but it ruins the game. Let’s delete the println! that outputs the secret number. Listing 2-6 shows the final code.
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs}}
至此,你已经成功构建了猜数字游戏。恭喜!
At this point, you’ve successfully built the guessing game. Congratulations!
总结 (Summary)
Summary
这个项目是通过实践向你介绍许多新的 Rust 概念的一种方式:let、match、函数、外部 crate 的使用等等。在接下来的几章中,你将更详细地学习这些概念。第 3 章介绍了大多数编程语言都有的概念,如变量、数据类型和函数,并展示了如何在 Rust 中使用它们。第 4 章探讨了所有权 (ownership),这是使 Rust 区别于其他语言的功能。第 5 章讨论了结构体 (structs) 和方法语法,而第 6 章则解释了枚举 (enums) 的工作原理。
This project was a hands-on way to introduce you to many new Rust concepts: let, match, functions, the use of external crates, and more. In the next few chapters, you’ll learn about these concepts in more detail. Chapter 3 covers concepts that most programming languages have, such as variables, data types, and functions, and shows how to use them in Rust. Chapter 4 explores ownership, a feature that makes Rust different from other languages. Chapter 5 discusses structs and method syntax, and Chapter 6 explains how enums work.