初识 Rust:为何这门语言与众不同 / Why Rust Stands Out
初识 Rust:为何这门语言与众不同 / Why Rust Stands Out
> 本章核心问题 / Chapter's Core Question > Rust 究竟解决了什么问题?它和其他主流语言相比有何独特之处? > *What problem does Rust actually solve, and how does it differ from other mainstream languages?*
1.1 一个始于个人项目却改变世界的语言 / A Personal Project That Changed the World
中文版
2006 年的某一天,加拿大不列颠哥伦比亚省维多利亚市的一位程序员 Graydon Hoare 像往常一样回到了自己的公寓。这位 Mozilla 公司的浏览器工程师打开电脑,准备修复一个让他困扰许久的小问题——他公寓楼里那台经常崩溃的电梯。
Graydon 后来回忆说,那台电梯的控制软件显然是用 C++ 写的,里面藏着无数内存管理的小毛病:偶尔忘记释放的内存、偶尔引用了不该引用的对象、偶尔因为某些边界条件就让整个系统直接蓝屏。这些问题在 C++ 程序员眼里是家常便饭,但对一个追求完美的工程师来说,却是难以忍受的折磨。
"我想,能不能用一种新的语言,让这些事情在编译期就被发现?" Graydon 当时这样想。
这个看似简单的想法,在接下来的几年里慢慢长成了一门完整的编程语言。Graydon 在 Mozilla 公司的支持下,于 2009 年开始正式公开 Rust 项目,并在 2010 年首次发布了编译器源码。Mozilla 公司当时正在寻找一种既能写出高性能 Web 浏览器引擎、又能避免 C++ 那些臭名昭著的内存安全漏洞的语言——Rust 恰好满足了这种需求。
真正让 Rust 走向世界的,是 Mozilla 在 2012 年启动的 Servo 浏览器引擎项目。这是一个用 Rust 编写的实验性浏览器引擎,旨在证明 Rust 能够胜任大规模系统软件的开发。Servo 项目不仅证明了 Rust 的能力,也帮助这门语言在实际工程中不断打磨、不断进化。在 Servo 项目的开发过程中,Rust 团队收到了大量来自一线工程师的反馈,据估算,仅仅是让 Servo 跑起来,Rust 团队就修复了上千个编译器和标准库的 bug。
2015 年 5 月 15 日,Rust 发布了 1.0 正式版,并做出了一个在当时看来非常大胆的承诺——"六周的发布周期 + 五年的 API 稳定性承诺"。这意味着 Rust 团队承诺在 5 年内不会破坏现有代码的兼容性。对于一门年轻的语言来说,这种对开发者的尊重是难能可贵的。它意味着:一旦你写下的代码在 Rust 1.0 上能跑,那么在 Rust 1.39 上也一定能跑(甚至在 2024 年的 Rust 1.79 上依然能跑)。这种稳定性让许多公司在生产环境中开始认真考虑采用 Rust。
此后,Rust 几乎以每年一个大版本(其实是每 6 周一个小版本)的速度稳定演进,并连续多年在 Stack Overflow 的"最受喜爱语言"调查中名列前茅。Linux 内核从 6.1 版本开始正式接纳 Rust 作为 C 之外的第二语言;Windows 操作系统也在逐步用 Rust 重写部分关键组件;AWS、Discord、Cloudflare、Dropbox 等公司都将 Rust 用于其核心基础设施。从浏览器到区块链,从嵌入式设备到云原生后端,Rust 的身影无处不在。
而这一切的起点,不过是 Graydon 想让一台电梯不再崩溃。
English Version
On a typical day in 2006, a programmer named Graydon Hoare returned to his apartment in Victoria, British Columbia, Canada. An engineer at Mozilla working on browser technology, he sat down at his computer to fix a small but persistent problem: the elevator in his apartment building kept crashing.
The elevator's control software was clearly written in C++, with all its notorious memory management pitfalls—occasionally forgotten memory frees, occasionally dangling references, occasionally edge cases that would bring the entire system down. To most C++ programmers, these were everyday occurrences. To a perfectionist engineer like Graydon, they were unbearable.
"I wondered if there could be a new language that would catch these problems at compile time," Graydon later recalled.
That simple idea would, over the following years, grow into an entire programming language. With Mozilla's support, Graydon publicly launched the Rust project in 2009, releasing the first compiler source code in 2010. Mozilla was actively searching for a language that could build a high-performance web browser engine while avoiding C++'s notorious memory safety vulnerabilities—Rust fit that need perfectly.
What truly brought Rust into the spotlight was Mozilla's Servo project, launched in 2012. Servo was an experimental browser engine written entirely in Rust, designed to prove that the language could handle large-scale systems software. Servo not only demonstrated Rust's capabilities but also helped refine and mature the language through real-world engineering. To get Servo running, the Rust team reportedly fixed over a thousand compiler and standard library bugs—an enormous investment that paid off in language stability.
On May 15, 2015, Rust released version 1.0, accompanied by a bold promise: "a six-week release cycle combined with a five-year API stability guarantee." This meant Rust would not break backward compatibility within that period. For a young language, this level of respect for developers was remarkable. It meant: if your code worked on Rust 1.0, it would work on Rust 1.39—and indeed still on Rust 1.79 in 2024. This stability allowed many companies to seriously consider adopting Rust in production.
Since then, Rust has evolved at a steady pace, with a new minor release every six weeks, and has consistently ranked among the most loved languages in Stack Overflow's annual developer survey. The Linux kernel officially adopted Rust as a second language alongside C starting from version 6.1. Windows has been gradually rewriting critical components in Rust. AWS, Discord, Cloudflare, Dropbox, and many other companies now use Rust in their core infrastructure. From browsers to blockchains, from embedded devices to cloud-native backends, Rust's fingerprints can be found everywhere.
And it all started with a programmer who just wanted his elevator to stop crashing.
---
1.2 三条腿的凳子:Rust 的核心设计哲学 / Three Legs of a Stool: Rust's Design Philosophy
中文版
如果你让 Rust 核心团队用一句话概括这门语言的设计目标,他们很可能会告诉你:安全、并发、实用——三个看似简单的词,背后却是一套极其精妙的设计哲学。我们把它比喻成"三条腿的凳子"——任何一条腿短了,凳子都坐不稳。
第一条腿:内存安全(Memory Safety),且没有垃圾回收(GC)
这是 Rust 最具辨识度的特征。它的目标是:消除一整类常见的、危害极大的 bug——用 C/C++ 写过程序的人对这些 bug 一定不陌生:空指针解引用(null pointer dereference)、悬垂引用(dangling reference)、二次释放(double free)、迭代器失效(iterator invalidation)、数据竞争(data race)等等。这些 bug 的共同特点是:
1. 编译器在编译时往往无法发现它们; 2. 它们的表现形式千变万化,调试极其困难; 3. 它们的危害极大——轻则程序崩溃,重则被攻击者利用,成为安全漏洞。
据统计,约 70% 的高危安全漏洞(CVE)都和内存安全问题有关(微软和 Google 的安全团队都曾公开过类似数据)。这意味着,写 C/C++ 就像在不戴安全带的情况下开赛车——速度很快,但一个小失误就可能车毁人亡。
传统上有两条路来避免这些 bug:
- **路线 A**:用 C/C++ 写代码,依靠程序员的小心和大量测试来"祈祷不出问题";
- **路线 B**:用 Java/Python/Go 等带 GC(垃圾回收器)的语言,让运行时自动管理内存。
两条路线都有问题:路线 A 不安全,路线 B 性能不可控(GC 会在你不希望的时候暂停你的程序)。
Rust 选择了一条全新的路线 C:在编译时通过一套严格的所有权(ownership)和借用(borrowing)规则来保证内存安全,无需运行时 GC。 这意味着 Rust 程序既没有 GC 暂停带来的性能抖动,也没有 C/C++ 那种"祈祷式编程"的安全隐患。
我们会在第 2 章深入讲解所有权系统的工作机制。现在你只需要记住:Rust 的内存安全是"在编译时强制保证的"——只要代码能编译通过,内存相关的 bug 就不存在。
第二条腿:零成本抽象(Zero-Cost Abstractions)
这是 Rust 继承自 C++ 的优秀传统。所谓"零成本抽象",是指:你不会因为使用了高级语言特性而付出运行时性能代价。 换句话说,抽象机制本身在编译后被完全消除,生成的机器码与手写的底层代码一样高效。
举个简单的例子。Rust 的迭代器(Iterator)允许你这样写代码:
```rust let sum: i32 = (1..=100) .filter(|x| x % 2 == 0) .map(|x| x * x) .sum(); ```
这段代码看起来像函数式编程,优雅而抽象。但 Rust 编译器(LLVM 后端)会把它优化成与手写循环完全等价的机器码——甚至在很多情况下更优。这种"高层抽象不损失性能"的能力,是 Rust 被称为"系统编程语言"的重要原因。
C++ 之父 Bjarne Stroustrup 曾经有一句名言:"What you don't use, you don't pay for. And further: What you do use, you couldn't hand code any better."(你不需要的,你不需要付出代价;你使用的,你也写不出比它更好的。)Rust 把这条原则贯彻得比 C++ 还彻底。
第三条腿:实用主义(Pragmatism)
Rust 团队有一个口头禅:"Rust 是一门工程语言,不是学术语言。" 这意味着 Rust 在设计时不会为了追求理论的优雅而牺牲工程上的实用性。
具体表现在:
- **稳定的 ABI 与 API**:一旦发布,就不会破坏现有代码;
- **丰富的工具链**:内置的 `cargo`(包管理 + 构建工具)、`rustfmt`(代码格式化)、`clippy`(代码 lint 工具)让开发体验极其流畅;
- **完善的文档**:`cargo doc` 自动生成美观的 API 文档,标准库文档里还包含大量示例代码;
- **友好的错误信息**:编译器会用通俗易懂的语言告诉你哪里错了、为什么错、怎么修("the compiler is your friend" 是 Rust 社区的流行语);
- **Cargo 注册中心 crates.io**:拥有超过 100,000 个第三方库("crates"),覆盖各种场景;
- **可选的 GC 与 unsafe**:在极少数情况下,你可以使用 `unsafe` 块绕过编译器的某些检查,但需要明确标注(这本身就是一个"安全警示牌")。
实用主义让 Rust 不仅仅是一门"理论上很美"的语言,更是一门"在实际项目里真的能用、好用、爱用"的语言。
三条腿的协同效应
这三条腿不是孤立的,它们之间有着精妙的协同:
- 内存安全让你**敢用**高层抽象(因为你知道底层不会出内存问题);
- 零成本抽象让你**愿意用**高层抽象(因为你知道不会损失性能);
- 实用主义让你**用得起来**(因为工具链和生态都跟得上)。
这就是为什么 Rust 能做到"鱼与熊掌兼得"——不是因为它耍了什么魔法,而是因为它在设计哲学上找到了一个正确的三角平衡点。
English Version
If you asked the Rust core team to summarize the language's design goals in one sentence, they would likely say: safe, concurrent, practical—three seemingly simple words that hide an extraordinarily refined design philosophy. We like to call this the "three legs of a stool"—if any leg is too short, the stool won't hold you up.
The First Leg: Memory Safety, Without Garbage Collection
This is Rust's most distinctive feature. Its goal: eliminate an entire class of common, dangerous bugs that any C/C++ programmer knows all too well: null pointer dereferences, dangling references, double frees, iterator invalidation, data races, and so on. These bugs share three characteristics:
1. Compilers often cannot detect them at compile time; 2. They manifest in infinitely varied ways, making them extremely hard to debug; 3. Their consequences are severe—ranging from program crashes to exploitable security vulnerabilities.
Statistics show that **approximately 70% of high-severity security vulnerabiliti