键盘快捷键

按 ← 或 → 在章节之间切换

按 S 或 / 搜索本书

按 ? 显示此帮助

按 Esc 隐藏此帮助

简介

注意:本版与 No Starch Press 出版的纸质书和电子书《Rust 程序设计语言》内容相同。

欢迎阅读《Rust 程序设计语言》,这是一本介绍 Rust 的入门书。Rust 编程语言能帮助你编写更快速、更可靠的软件。在编程语言设计中,高层次的易用性与底层控制往往难以兼得;Rust 正面挑战了这种冲突。通过兼顾强大的技术能力与出色的开发体验,Rust 让你能够控制底层细节(例如内存使用),同时不必承受传统上与这种控制相伴的种种麻烦。

Rust 适合哪些人

Rust 适合许多人,原因各不相同。下面来看看几个最重要的群体。

开发团队

事实证明,Rust 是不同系统编程经验水平的开发者组成大型团队时,一种高效的协作工具。底层代码容易出现各种细微的错误;在大多数其他语言中,只有通过大量测试以及有经验的开发者仔细审查代码,才能发现这些问题。在 Rust 中,编译器扮演守门人的角色,会拒绝编译包含这类隐蔽错误(包括并发错误)的代码。与编译器协作,团队便能把时间用于关注程序逻辑,而不是四处追查错误。

Rust 也为系统编程领域带来了现代化的开发工具:

  • Cargo 是 Rust 自带的依赖管理器和构建工具,让你可以轻松地添加、编译和管理依赖,并在整个 Rust 生态系统中保持一致。
  • rustfmt 格式化工具确保不同开发者编写的代码风格一致。
  • Rust 语言服务器为集成开发环境(IDE)提供代码补全和行内错误消息等功能。

借助这些以及 Rust 生态系统中的其他工具,开发者在编写系统级代码时也能保持高效。

学生

Rust 适合学生,以及所有对学习系统概念感兴趣的人。许多人借助 Rust 学习了操作系统开发等主题。Rust 社区非常友好,也乐于回答学生的问题。Rust 团队通过本书等努力,希望让更多人更容易理解系统概念,尤其是刚开始学习编程的人。

企业

数以百计的大大小小的公司都在生产环境中使用 Rust,应用领域包括命令行工具、Web 服务、DevOps 工具、嵌入式设备、音视频分析与转码、加密货币、生物信息学、搜索引擎、物联网应用、机器学习,甚至 Firefox 浏览器的许多重要组成部分。

开源开发者

Rust 也适合希望构建 Rust 编程语言、社区、开发者工具和库的人。我们非常欢迎你为 Rust 语言做出贡献。

重视速度与稳定性的人

Rust 适合追求语言速度与稳定性的人。这里的速度既指 Rust 代码的运行速度,也指 Rust 帮助你编写程序的速度。Rust 编译器的检查能确保程序在添加功能和重构后仍保持稳定。这与缺少此类检查的语言形成对比:那些语言中的遗留代码十分脆弱,开发者往往不敢修改。Rust 致力于实现零成本抽象,也就是将高层次功能编译为与手写底层代码同样快速的代码,从而让安全代码也能拥有高性能。

Rust 还希望服务于许多其他用户;这里提到的只是其中一些最主要的群体。总的来说,Rust 最大的目标是消除程序员几十年来不得不接受的取舍,同时提供安全性与生产力、速度与易用性。试试 Rust,看看它的设计是否适合你。

本书适合哪些读者

本书假设你已经使用其他编程语言写过代码,但不假设你使用过哪一种。我们努力让来自各种编程背景的读者都能理解本书内容。我们不会花很多时间解释编程是什么,或如何思考编程。如果你完全没有编程经验,最好先阅读一本专门介绍编程的书。

如何使用本书

一般来说,本书假设你会从头到尾按顺序阅读。后面的章节会建立在前面章节的概念之上;前面的章节可能不会深入某个主题的所有细节,而会在后续章节中继续讲解。

本书包含两类章节:概念章节和项目章节。概念章节会介绍 Rust 的某个方面;项目章节则会带你一起构建小程序,应用目前学到的知识。第 2、12 和 21 章是项目章节,其余章节都是概念章节。

第 1 章介绍如何安装 Rust、编写“Hello, world!”程序,以及如何使用 Rust 的包管理器和构建工具 Cargo。第 2 章通过实践介绍 Rust 编程,带你逐步构建一个猜数字游戏。本章会从较高层次讲解相关概念,后续章节会提供更多细节。如果你想马上动手实践,可以从第 2 章开始。如果你是喜欢先掌握所有细节再继续的严谨型学习者,可以跳过第 2 章,直接阅读介绍 Rust 常见功能的第 3 章;之后想做项目、应用所学细节时,再回来阅读第 2 章。

第 4 章介绍 Rust 的所有权系统。第 5 章讨论结构体和方法。第 6 章讲解枚举、match 表达式,以及 if let 和 let...else 控制流结构。你将使用结构体和枚举创建自定义类型。

第 7 章介绍 Rust 的模块系统,以及用于组织代码和公共应用程序编程接口(API)的可见性规则。第 8 章讨论标准库提供的一些常见集合数据结构:向量、字符串和哈希映射。第 9 章探索 Rust 的错误处理理念与技术。

第 10 章深入介绍泛型、trait 和生命周期,让你能够定义适用于多种类型的代码。第 11 章全面讲解测试;即使 Rust 提供了安全性保证,测试仍然是确保程序逻辑正确的必要手段。第 12 章将带你实现命令行工具 grep 的部分功能,用来在文件中搜索文本。为此,我们会运用前面章节讨论的许多概念。

第 13 章探索闭包和迭代器,这些功能源自函数式编程语言。第 14 章会更深入地考察 Cargo,并讨论与他人分享库的最佳实践。第 15 章讨论标准库提供的智能指针,以及使其发挥作用的 trait。

第 16 章将介绍不同的并发编程模型,并讲解 Rust 如何帮助你放心地进行多线程编程。第 17 章将在此基础上探讨 Rust 的 async 和 await 语法,以及它们支持的任务、Future、流和轻量级并发模型。

第 18 章会比较 Rust 习惯用法与你可能熟悉的面向对象编程原则。第 19 章是关于模式和模式匹配的参考指南;它们是在 Rust 程序中表达思想的强大方式。第 20 章汇集了许多高级主题,包括不安全 Rust、宏,以及关于生命周期、trait、类型、函数和闭包的更多内容。

第 21 章将通过一个项目收尾:实现一个底层的多线程 Web 服务器!

最后,部分附录以更接近参考手册的形式提供有关这门语言的实用信息。附录 A介绍 Rust 关键字;附录 B介绍 Rust 运算符和符号;附录 C介绍标准库提供的可派生 trait;附录 D介绍一些实用开发工具;附录 E解释 Rust 版本。附录 F列出了本书的译本,附录 G则介绍 Rust 的开发方式以及 nightly Rust。

阅读本书没有固定的正确方式:想跳到后面读也完全可以!如果遇到困惑,你可能需要回到前面的章节。不过,选择最适合自己的方式就好。

code be fast code as well.

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

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

In Chapter 21, we’ll complete a project in which we’ll implement a low-level multithreaded web server!

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 也会帮助你辨认那些本就无法正常运行的代码:

Ferris含义
带着问号的 Ferris这段代码无法编译!
举起双手的 Ferris这段代码会触发 panic!
抬起一只爪子耸肩的 Ferris这段代码不会产生预期行为。

大多数情况下,我们会引导你找到无法编译代码的正确版本。

源代码

生成本书所用的源文件可以在 GitHub 上找到。