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-01T14:14:40Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 43a390d84480ff0f5ae94b422cc07a131b97784d6a0b9943f8401a235090b39c source_path: ch12-02-reading-a-file.md workflow: 16

读取文件 (Reading a File)

Reading a File

现在我们将添加读取 file_path 参数中指定的文件功能。首先,我们需要一个示例文件来测试它:我们将使用一个包含少量文本、分布在多行且有一些重复单词的文件。示例 12-3 中 Emily Dickinson 的一首诗会非常合适!在项目根目录下创建一个名为 poem.txt 的文件,并输入这首诗 “I’m Nobody! Who are you?”。

Now we’ll add functionality to read the file specified in the file_path argument. First, we need a sample file to test it with: We’ll use a file with a small amount of text over multiple lines with some repeated words. Listing 12-3 has an Emily Dickinson poem that will work well! Create a file called poem.txt at the root level of your project, and enter the poem “I’m Nobody! Who are you?”

{{#include ../listings/ch12-an-io-project/listing-12-03/poem.txt}}

文本准备好后,编辑 src/main.rs 并添加读取文件的代码,如示例 12-4 所示。

With the text in place, edit src/main.rs and add code to read the file, as shown in Listing 12-4.

{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-04/src/main.rs:here}}

首先,我们通过 use 语句引入标准库的相关部分:我们需要 std::fs 来处理文件。

First, we bring in a relevant part of the standard library with a use statement: We need std::fs to handle files.

main 中,新语句 fs::read_to_string 接收 file_path ,打开该文件,并返回一个包含文件内容的 std::io::Result<String> 类型的值。

In main, the new statement fs::read_to_string takes the file_path, opens that file, and returns a value of type std::io::Result<String> that contains the file’s contents.

之后,我们再次添加了一条临时的 println! 语句,用于在文件读取后打印 contents 的值,以便检查程序目前的工作情况。

After that, we again add a temporary println! statement that prints the value of contents after the file is read so that we can check that the program is working so far.

让我们使用任意字符串作为第一个命令行参数(因为我们还没有实现搜索部分),并将 poem.txt 文件作为第二个参数来运行这段代码:

Let’s run this code with any string as the first command line argument (because we haven’t implemented the searching part yet) and the poem.txt file as the second argument:

{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-04/output.txt}}

太棒了!代码读取并打印了文件内容。但代码有一些缺陷。目前,main 函数承担了多重职责:通常情况下,如果每个函数只负责一个概念,函数会更清晰且更易于维护。另一个问题是我们对错误的处理不够完善。程序目前还很小,所以这些缺陷不是大问题,但随着程序的增长,要干净地修复它们会变得更加困难。在开发程序时尽早开始重构是一个好习惯,因为重构少量代码要容易得多。接下来我们将进行重构。

Great! The code read and then printed the contents of the file. But the code has a few flaws. At the moment, the main function has multiple responsibilities: Generally, functions are clearer and easier to maintain if each function is responsible for only one idea. The other problem is that we’re not handling errors as well as we could. The program is still small, so these flaws aren’t a big problem, but as the program grows, it will be harder to fix them cleanly. It’s a good practice to begin refactoring early on when developing a program because it’s much easier to refactor smaller amounts of code. We’ll do that next.