x-i18n: generated_at: “2026-03-01T14:12:42Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 0558d29b101abf718e8944d8ac3687282579dbdc1e3626c3eb2c18f9badf1495 source_path: ch12-00-an-io-project.md workflow: 16
I/O 项目:构建命令行程序 (An I/O Project: Building a Command Line Program)
An I/O Project: Building a Command Line Program
本章是对你目前所学技能的回顾,也是对一些标准库功能的探索。我们将构建一个与文件和命令行输入/输出交互的命令行工具,以练习你已经掌握的一些 Rust 概念。
This chapter is a recap of the many skills you’ve learned so far and an exploration of a few more standard library features. We’ll build a command line tool that interacts with file and command line input/output to practice some of the Rust concepts you now have under your belt.
Rust 的速度、安全性、单一二进制输出以及跨平台支持使其成为创建命令行工具的理想语言,因此在我们的项目中,我们将制作我们自己的经典命令行搜索工具 grep (globally search a regular expression and print) 版本。在最简单的使用案例中,grep 在指定文件中搜索指定的字符串。为此,grep 将文件路径和字符串作为其参数。然后,它读取文件,查找该文件中包含字符串参数的行,并打印这些行。
Rust’s speed, safety, single binary output, and cross-platform support make it
an ideal language for creating command line tools, so for our project, we’ll
make our own version of the classic command line search tool grep
(globally search a regular expression and print). In the
simplest use case, grep searches a specified file for a specified string. To
do so, grep takes as its arguments a file path and a string. Then, it reads
the file, finds lines in that file that contain the string argument, and prints
those lines.
在此过程中,我们将展示如何让我们的命令行工具使用许多其他命令行工具使用的终端功能。我们将读取环境变量的值,以允许用户配置我们工具的行为。我们还将把错误消息打印到标准错误控制台流 (stderr) 而不是标准输出 (stdout),以便用户可以将成功输出重定向到文件,同时仍能在屏幕上看到错误消息。
Along the way, we’ll show how to make our command line tool use the terminal
features that many other command line tools use. We’ll read the value of an
environment variable to allow the user to configure the behavior of our tool.
We’ll also print error messages to the standard error console stream (stderr)
instead of standard output (stdout) so that, for example, the user can
redirect successful output to a file while still seeing error messages onscreen.
Rust 社区成员 Andrew Gallant 已经创建了一个功能齐全、速度极快的 grep 版本,称为 ripgrep。相比之下,我们的版本将相当简单,但本章将为你提供理解 ripgrep 等实际项目所需的一些背景知识。
One Rust community member, Andrew Gallant, has already created a fully
featured, very fast version of grep, called ripgrep. By comparison, our
version will be fairly simple, but this chapter will give you some of the
background knowledge you need to understand a real-world project such as
ripgrep.
我们的 grep 项目将结合你目前学到的一系列概念:
Our grep project will combine a number of concepts you’ve learned so far:
-
组织代码(第 7 章)
-
使用向量和字符串(第 8 章)
-
处理错误(第 9 章)
-
在适当的地方使用特征和生命周期(第 10 章)
-
编写测试(第 11 章)
-
Organizing code (Chapter 7)
-
Using vectors and strings (Chapter 8)
-
Handling errors (Chapter 9)
-
Using traits and lifetimes where appropriate (Chapter 10)
-
Writing tests (Chapter 11)
我们还将简要介绍闭包 (closures)、迭代器 (iterators) 和特征对象 (trait objects),第 13 章 和 第 18 章 将对这些内容进行详细介绍。
We’ll also briefly introduce closures, iterators, and trait objects, which Chapter 13 and Chapter 18 will cover in detail.