x-i18n: generated_at: “2026-03-01T09:17:47Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 2eba711175d633b5c6bf2585bcb6382fef4eec2b39f18689a1a2b9134dd9d273 source_path: ch00-00-introduction.md workflow: 16
导言 (Introduction)
Introduction
注意:本书的版本与 No Starch Press 出版的纸质书和电子书 《Rust 程序设计语言》(The Rust Programming Language) 相同。
Note: This edition of the book is the same as The Rust Programming Language available in print and ebook format from No Starch Press.
欢迎阅读《Rust 程序设计语言》(The Rust Programming Language),这是一本关于 Rust 的入门书籍。Rust 程序设计语言可以帮助你编写更快、更可靠的软件。在编程语言设计中,高级的人机工程学 (ergonomics) 和底层的控制力往往是相互矛盾的;Rust 挑战了这种冲突。通过平衡强大的技术能力和卓越的开发者体验,Rust 让你能够控制底层细节(如内存使用),而无需承受传统上与此类控制相关的各种烦恼。
Welcome to The Rust Programming Language, an introductory book about Rust. The Rust programming language helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option to control low-level details (such as memory usage) without all the hassle traditionally associated with such control.
谁适合使用 Rust (Who Rust Is For)
Who Rust Is For
出于各种原因,Rust 对许多人来说都是理想的选择。让我们来看看其中最重要的几个群体。
Rust is ideal for many people for a variety of reasons. Let’s look at a few of the most important groups.
开发团队 (Teams of Developers)
Teams of Developers
事实证明,Rust 是一个高效的工具,可用于具有不同系统编程知识水平的大型开发团队之间的协作。底层代码容易出现各种微妙的 Bug,在大多数其他语言中,这些 Bug 只能通过大量的测试和经验丰富的开发者进行仔细的代码审查才能发现。在 Rust 中,编译器扮演了“守门员”的角色,它会拒绝编译包含这些难以捉摸的 Bug(包括并发 Bug)的代码。通过与编译器配合工作,团队可以将时间集中在程序的逻辑上,而不是去追踪 Bug。
Rust is proving to be a productive tool for collaborating among large teams of developers with varying levels of systems programming knowledge. Low-level code is prone to various subtle bugs, which in most other languages can only be caught through extensive testing and careful code review by experienced developers. In Rust, the compiler plays a gatekeeper role by refusing to compile code with these elusive bugs, including concurrency bugs. By working alongside the compiler, the team can spend its time focusing on the program’s logic rather than chasing down bugs.
Rust 还为系统编程领域带来了现代化的开发者工具:
Rust also brings contemporary developer tools to the systems programming world:
- Cargo 是内置的依赖管理器和构建工具,它使得在 Rust 生态系统中添加、编译和管理依赖项变得轻松且一致。
- Cargo, the included dependency manager and build tool, makes adding, compiling, and managing dependencies painless and consistent across the Rust ecosystem.
rustfmt格式化工具确保了开发者之间统一的代码风格。- The
rustfmtformatting tool ensures a consistent coding style across developers. - Rust 语言服务器 (Rust Language Server) 为集成开发环境 (IDE) 集成提供支持,实现代码补全和内联错误消息。
- The Rust Language Server powers integrated development environment (IDE) integration for code completion and inline error messages.
通过使用 Rust 生态系统中的这些以及其他工具,开发者在编写系统级代码时可以保持高效。
By using these and other tools in the Rust ecosystem, developers can be productive while writing systems-level code.
学生 (Students)
Students
Rust 适用于学生和那些对学习系统概念感兴趣的人。通过使用 Rust,许多人学习了诸如操作系统开发之类的课题。社区非常友好,乐于回答学生的问题。通过像本书这样的努力,Rust 团队希望让更多人(特别是编程新手)能够更容易地理解系统概念。
Rust is for students and those who are interested in learning about systems concepts. Using Rust, many people have learned about topics like operating systems development. The community is very welcoming and happy to answer students’ questions. Through efforts such as this book, the Rust teams want to make systems concepts more accessible to more people, especially those new to programming.
公司 (Companies)
Companies
成百上千家大小公司在生产环境中使用 Rust 完成各种任务,包括命令行工具、Web 服务、DevOps 工具、嵌入式设备、音视频分析和转码、加密货币、生物信息学、搜索引擎、物联网应用、机器学习,甚至 Firefox 浏览器的主要部分。
Hundreds of companies, large and small, use Rust in production for a variety of tasks, including command line tools, web services, DevOps tooling, embedded devices, audio and video analysis and transcoding, cryptocurrencies, bioinformatics, search engines, Internet of Things applications, machine learning, and even major parts of the Firefox web browser.
开源开发者 (Open Source Developers)
Open Source Developers
Rust 适用于那些想要构建 Rust 编程语言、社区、开发工具和库的人。我们非常欢迎你为 Rust 语言做出贡献。
Rust is for people who want to build the Rust programming language, community, developer tools, and libraries. We’d love to have you contribute to the Rust language.
重视速度与稳定性的开发者 (People Who Value Speed and Stability)
People Who Value Speed and Stability
Rust 适用于那些渴望语言具有速度和稳定性的开发者。所谓速度,我们既指 Rust 代码运行的速度,也指 Rust 让你编写程序的效率。Rust 编译器的检查确保了在添加特性和进行重构时的稳定性。这与那些没有这些检查的语言中脆弱的遗留代码形成鲜明对比,开发者往往不敢修改它们。通过追求零成本抽象 (zero-cost abstractions)——即编译成与手动编写的代码一样快的底层代码的高级特性——Rust 致力于让安全的代码同样也是快速的代码。
Rust is for people who crave speed and stability in a language. By speed, we mean both how quickly Rust code can run and the speed at which Rust lets you write programs. The Rust compiler’s checks ensure stability through feature additions and refactoring. This is in contrast to the brittle legacy code in languages without these checks, which developers are often afraid to modify. By striving for zero-cost abstractions—higher-level features that compile to lower-level code as fast as code written manually—Rust endeavors to make safe code be fast code as well.
Rust 语言也希望支持许多其他用户;这里提到的仅仅是一些最大的利益相关者。总的来说,Rust 最大的雄心是通过提供安全性“与”生产力、速度“与”人机工程学,来消除程序员几十年来不得不接受的权衡。尝试一下 Rust,看看它的选择是否适合你。
The Rust language hopes to support many other users as well; those mentioned here are merely some of the biggest stakeholders. Overall, Rust’s greatest ambition is to eliminate the trade-offs that programmers have accepted for decades by providing safety and productivity, speed and ergonomics. Give Rust a try, and see if its choices work for you.
本书读者对象 (Who This Book Is For)
Who This Book Is For
本书假设你已经用另一种编程语言写过代码,但并不对具体是哪种语言做任何假设。我们努力使这些内容能够被具有各种编程背景的人广泛接受。我们不会花很多时间讨论什么是编程,或者如何思考编程。如果你完全是编程新手,阅读专门提供编程入门的书籍会对你更有帮助。
This book assumes that you’ve written code in another programming language, but it doesn’t make any assumptions about which one. We’ve tried to make the material broadly accessible to those from a wide variety of programming backgrounds. We don’t spend a lot of time talking about what programming is or how to think about it. If you’re entirely new to programming, you would be better served by reading a book that specifically provides an introduction to programming.
如何使用本书 (How to Use This Book)
How to Use This Book
总的来说,本书假设你是按照从前到后的顺序阅读的。后面的章节建立在前面章节的概念之上,而前面的章节可能不会深入探讨某个特定主题的细节,但在后面的章节中会重新讨论该主题。
In general, this book assumes that you’re reading it in sequence from front to back. Later chapters build on concepts in earlier chapters, and earlier chapters might not delve into details on a particular topic but will revisit the topic in a later chapter.
你会在本书中发现两种章节:概念章节和项目章节。在概念章节中,你将学习 Rust 的某个方面。在项目章节中,我们将一起构建小程序,应用你目前学到的知识。第 2 章、第 12 章和第 21 章是项目章节;其余的是概念章节。
You’ll find two kinds of chapters in this book: concept chapters and project chapters. In concept chapters, you’ll learn about an aspect of Rust. In project chapters, we’ll build small programs together, applying what you’ve learned so far. Chapter 2, Chapter 12, and Chapter 21 are project chapters; the rest are concept chapters.
第 1 章 解释了如何安装 Rust,如何编写“Hello, world!”程序,以及如何使用 Cargo(Rust 的包管理器和构建工具)。第 2 章 是编写 Rust 程序的动手实践入门,让你构建一个猜数字游戏。在这里,我们从宏观层面介绍概念,后面的章节将提供额外的细节。如果你想马上动手尝试,第 2 章就是为你准备的。如果你是一个特别严谨的学习者,更喜欢在进入下一步之前学习每一个细节,你可能想跳过第 2 章,直接阅读 第 3 章,该章涵盖了与其他编程语言相似的 Rust 特性;然后,当你想要开展一个应用所学细节的项目时,可以再回到第 2 章。
Chapter 1 explains how to install Rust, how to write a “Hello, world!” program, and how to use Cargo, Rust’s package manager and build tool. Chapter 2 is a hands-on introduction to writing a program in Rust, having you build up a number-guessing game. Here, we cover concepts at a high level, and later chapters will provide additional detail. If you want to get your hands dirty right away, Chapter 2 is the place for that. If you’re a particularly meticulous learner who prefers to learn every detail before moving on to the next, you might want to skip Chapter 2 and go straight to Chapter 3, which covers Rust features that are similar to those of other programming languages; then, you can return to Chapter 2 when you’d like to work on a project applying the details you’ve learned.
在 第 4 章 中,你将学习 Rust 的所有权系统。第 5 章 讨论结构体 (structs) 和方法 (methods)。第 6 章 涵盖枚举 (enums)、match 表达式,以及 if let 和 let...else 控制流结构。你将使用结构体和枚举来创建自定义类型。
In Chapter 4, you’ll learn about Rust’s ownership system. Chapter 5
discusses structs and methods. Chapter 6 covers enums, match expressions,
and the if let and let...else control flow constructs. You’ll use structs
and enums to make custom types.
在 第 7 章 中,你将学习 Rust 的模块系统,以及用于组织代码及其公共应用编程接口 (API) 的私有性规则。第 8 章 讨论标准库提供的一些常见集合数据结构:Vector、String 和哈希映射 (hash maps)。第 9 章 探索 Rust 的错误处理哲学和技术。
In Chapter 7, you’ll learn about Rust’s module system and about privacy rules for organizing your code and its public application programming interface (API). Chapter 8 discusses some common collection data structures that the standard library provides: vectors, strings, and hash maps. Chapter 9 explores Rust’s error-handling philosophy and techniques.
第 10 章 深入研究泛型 (generics)、Traits 和生命周期 (lifetimes),它们赋予你定义适用于多种类型的代码的能力。第 11 章 全面介绍测试,即使有 Rust 的安全保证,测试对于确保程序逻辑正确也是必要的。在 第 12 章 中,我们将实现 grep 命令行工具的功能子集,用于在文件中搜索文本。为此,我们将使用前面章节中讨论的许多概念。
Chapter 10 digs into generics, traits, and lifetimes, which give you the
power to define code that applies to multiple types. Chapter 11 is all
about testing, which even with Rust’s safety guarantees is necessary to ensure
that your program’s logic is correct. In Chapter 12, we’ll build our own
implementation of a subset of functionality from the grep command line tool
that searches for text within files. For this, we’ll use many of the concepts
we discussed in the previous chapters.
第 13 章 探索闭包 (closures) 和迭代器 (iterators):这些 Rust 特性源自函数式编程语言。在 第 14 章 中,我们将更深入地研究 Cargo,并讨论与他人分享你的库的最佳实践。第 15 章 讨论标准库提供的智能指针以及实现其功能的 Traits。
Chapter 13 explores closures and iterators: features of Rust that come from functional programming languages. In Chapter 14, we’ll examine Cargo in more depth and talk about best practices for sharing your libraries with others. Chapter 15 discusses smart pointers that the standard library provides and the traits that enable their functionality.
在 第 16 章 中,我们将介绍并发编程的不同模型,并讨论 Rust 如何帮助你无畏地进行多线程编程。在 第 17 章 中,我们在此基础上探索 Rust 的 async 和 await 语法,以及任务 (tasks)、Futures 和 Streams,以及它们所实现的轻量级并发模型。
In Chapter 16, we’ll walk through different models of concurrent programming and talk about how Rust helps you program in multiple threads fearlessly. In Chapter 17, we build on that by exploring Rust’s async and await syntax, along with tasks, futures, and streams, and the lightweight concurrency model they enable.
第 18 章 探讨 Rust 惯用法如何与你可能熟悉的面向对象编程原则进行对比。第 19 章 是关于模式和模式匹配的参考,它们是在整个 Rust 程序中表达想法的强大方式。第 20 章 包含了一系列感兴趣的高级主题,包括不安全 Rust (unsafe Rust)、宏 (macros),以及更多关于生命周期、Traits、类型、函数和闭包的内容。
Chapter 18 looks at how Rust idioms compare to object-oriented programming principles you might be familiar with. Chapter 19 is a reference on patterns and pattern matching, which are powerful ways of expressing ideas throughout Rust programs. Chapter 20 contains a smorgasbord of advanced topics of interest, including unsafe Rust, macros, and more about lifetimes, traits, types, functions, and closures.
在 第 21 章 中,我们将完成一个项目,在其中我们将实现一个底层的多线程 Web 服务器!
In Chapter 21, we’ll complete a project in which we’ll implement a low-level multithreaded web server!
最后,一些附录以更类似参考资料的格式包含了关于该语言的有用信息。附录 A 涵盖 Rust 的关键字,附录 B 涵盖 Rust 的运算符和符号,附录 C 涵盖标准库提供的可派生 Traits,附录 D 涵盖一些实用的开发工具,附录 E 解释 Rust 版本。在 附录 F 中,你可以找到本书的翻译版,在 附录 G 中,我们将介绍 Rust 是如何开发的以及什么是每夜版 (nightly) Rust。
Finally, some appendixes contain useful information about the language in a more reference-like format. Appendix A covers Rust’s keywords, Appendix B covers Rust’s operators and symbols, Appendix C covers derivable traits provided by the standard library, Appendix D covers some useful development tools, and Appendix E explains Rust editions. In Appendix F, you can find translations of the book, and in Appendix G we’ll cover how Rust is made and what nightly Rust is.
阅读本书没有错误的方式:如果你想跳着看,那就去吧!如果你感到任何困惑,可能需要跳回前面的章节。但请采取任何适合你的方式。
There is no wrong way to read this book: If you want to skip ahead, go for it! You might have to jump back to earlier chapters if you experience any confusion. But do whatever works for you.
学习 Rust 过程中一个重要的部分是学习如何阅读编译器显示的错误消息:这些将引导你编写出可工作的代码。因此,我们将提供许多无法编译的示例,以及编译器在每种情况下会显示给你的错误消息。请记住,如果你输入并运行一个随机示例,它可能无法编译!请确保阅读上下文文字,以查看你尝试运行的示例是否旨在报错。在大多数情况下,我们会引导你获得任何无法编译的代码的正确版本。费里斯 (Ferris) 也会帮助你区分那些不打算运行的代码:
An important part of the process of learning Rust is learning how to read the error messages the compiler displays: These will guide you toward working code. As such, we’ll provide many examples that don’t compile along with the error message the compiler will show you in each situation. Know that if you enter and run a random example, it may not compile! Make sure you read the surrounding text to see whether the example you’re trying to run is meant to error. In most situations, we’ll lead you to the correct version of any code that doesn’t compile. Ferris will also help you distinguish code that isn’t meant to work:
| 费里斯 (Ferris) | 含义 (Meaning) |
|---|---|
| 这段代码无法编译! (This code does not compile!) | |
| 这段代码会发生 Panic! (This code panics!) | |
| 这段代码不会产生预期的行为。 (This code does not produce the desired behavior.) |
在大多数情况下,我们将引导你得到任何无法编译代码的正确版本。
In most situations, we’ll lead you to the correct version of any code that doesn’t compile.
源代码 (Source Code)
Source Code
生成本书的源文件可以在 GitHub 上找到。
The source files from which this book is generated can be found on GitHub.