# Rust（编程语言）

**Rust** 是一门系统编程语言，它通过所有权系统在编译期强制保证内存安全，既不需要垃圾回收器，也不会因所有权检查产生运行时开销。编译器在代码运行之前检查 Rust 的所有权和借用规则，在安全代码中提供强有力的防护，防止内存安全错误和数据竞争。<sup>[1](https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html)</sup><sup> • </sup><sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup> 该语言每六周发布一个新版本，从 Rust 1.0.0 开始。<sup>[3](https://doc.rust-lang.org/stable/reference/index.html)</sup> [JetBrains](https://www.edgepedia.org/zh/jetbrains) 估计，在截至 2025 年 5 月的 12 个月内，有 2,267,000 名开发者使用了 Rust，其中 709,000 人将其列为主要语言。<sup>[4](https://blog.jetbrains.com/rust/2025/05/13/is-rust-the-future-of-programming/)</sup>

| 关键事实 | 详情 |
|---|---|
| 安全保障 | 安全 Rust 可防止不安全的缓冲区溢出、释放后使用、空指针解引用和数据竞争；所有权检查没有运行时开销。<sup>[1](https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html)</sup><sup> • </sup><sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup> |
| 借用规则 | 在任何给定时刻，一个值只能有一个可变引用或任意数量的不可变引用，且引用必须始终有效。<sup>[5](https://doc.rust-lang.org/nightly/book/ch04-02-references-and-borrowing.html)</sup> |
| 不安全代码占比 | 在相隔 16 个月进行的两次 crate 调查中，分别有 29% 和 23.6% 的 Rust crate 包含不安全代码。<sup>[6](https://arxiv.org/html/2404.02230v2)</sup> |
| 生态系统缺陷 | Rudra 在 6.5 小时内扫描了 crates.io 上全部 43,000 个包，发现 264 个此前未知的内存安全缺陷，产生了 76 条 CVE 记录。<sup>[7](https://dl.acm.org/doi/pdf/10.1145/3477132.3483570)</sup> |
| 性能 | Rust 和 C++ 占据性能最高梯队，在大多数基准测试的系统编程场景中测得差异低于 5%。<sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup><sup> • </sup><sup>[8](https://github.laiyagushi.com/cschladetsch/CppRustComparison)</sup> |
| 采用情况 | 截至 2025 年 5 月的一年内有 2,267,000 名开发者使用 Rust；72% 的 Stack Overflow 2025 受访者表示欣赏它，为所有语言中最高。<sup>[4](https://blog.jetbrains.com/rust/2025/05/13/is-rust-the-future-of-programming/)</sup><sup> • </sup><sup>[9](https://survey.stackoverflow.co/2025/technology)</sup> |
| 最大抱怨 | 编译速度慢是 2024 年 Rust 状况调查中首要的生产力限制因素。<sup>[10](https://blog.rust-lang.org/2025/02/13/2024-State-Of-Rust-Survey-results/)</sup> |

## 所有权模型：Rust 如何实现内存安全

所有权基于三条规则：Rust 中的每个值都有一个所有者，同一时间只能有一个所有者，当所有者离开作用域时该值被丢弃。<sup>[1](https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html)</sup> 当变量离开作用域时，Rust 自动调用 drop 来清理堆内存。这种自动清理消除了双重释放错误，即内存被释放两次；双重释放可能导致内存损坏和安全漏洞，而所有权使这类错误在安全代码中无法表达。<sup>[1](https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html)</sup>

**借用。** 代码可以不转移所有权，而是获取一个引用。裸指针不做这样的承诺，而引用在存在期间必须始终指向给定类型的有效值，创建引用的行为称为借用。<sup>[5](https://doc.rust-lang.org/nightly/book/ch04-02-references-and-borrowing.html)</sup> 借用规则是：在任何给定时刻，一个值只能有一个可变引用或任意数量的不可变引用，且引用必须始终有效。编译器保证引用永远不会悬垂。<sup>[5](https://doc.rust-lang.org/nightly/book/ch04-02-references-and-borrowing.html)</sup> 多个共享引用或一个可变引用、但两者不能同时存在的规则，正是编译器得以证明不存在数据竞争的依据。<sup>[11](https://softwarepatternslexicon.com/rust/appendices/comparing-rust-with-other-systems-programming-languages/)</sup>

当所有权发生移动时，例如将一个非 Copy 值移入函数，原始变量绑定就不能再使用，除非有意改为借用该值，而借用检查器会在代码运行之前捕获此类错误。<sup>[12](https://blog.jetbrains.com/rust/2025/12/16/rust-vs-cpp-comparison-for-2026/)</sup> 由于所有这些检查都在编译期进行，所有权特性不会在程序运行时拖慢程序。<sup>[1](https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html)</sup>

## 实践中的生命周期

每个引用都有一个生命周期，即其有效的范围。生命周期通常是隐式的、由编译器推断的，其主要目的是防止悬垂引用，悬垂引用会导致程序引用非其意图引用的数据。<sup>[13](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)</sup> 借用检查器比较作用域以确定所有借用是否有效，拒绝那些被引用对象的存活时间不够长的程序。<sup>[13](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)</sup>

一个常见的误解是，编写生命周期标注会改变某物的存活时长。事实并非如此：标注描述的是多个引用的生命周期之间的关系，分析完全在编译期进行，不影响运行时性能。省略规则允许在常见情况下省略大多数标注。<sup>[13](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)</sup>

与 C++ 的实际差异是鲜明的。返回局部变量引用的函数在 C++ 中可以编译，但在运行时导致未定义行为；在 Rust 中，借用检查器将其识别为生命周期错误并拒绝编译该代码。<sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup> 行业经验表明，借用检查器本身并不是新开发者面临的最大障碍。Grab 的工程师发现，过早引入生命周期——在需要之前就添加显式生命周期标注——才是主要的陷阱。<sup>[14](https://engineering.grab.com/counter-service-how-we-rewrote-it-in-rust/)</sup>

## 不安全 Rust 与安全保障的边界

借用检查器保证引用不能比其所有者变量存活更久，从而防止释放后使用，并保证共享引用和可变引用永不共存。不安全代码允许在这些保证之外进行操作，即使在经过专家审查的不安全代码中也发现了缺陷，包括 Rust 标准库中的两个、官方 futures 库中的一个，以及 Rust 编译器本身中的一个。<sup>[7](https://dl.acm.org/doi/pdf/10.1145/3477132.3483570)</sup>

不安全代码很常见但分布集中。相隔 16 个月进行的两次 crate 调查发现，29% 和 23.6% 的 Rust crate 包含不安全代码，其中大多数不安全代码块很小，不安全函数调用是最常见的不安全操作。<sup>[6](https://arxiv.org/html/2404.02230v2)</sup> PLDI 2020 的一项研究手动检查了五个开源 Rust 项目、五个广泛使用的库、两个安全数据库和标准库中的 850 处不安全代码用法和 170 个缺陷，结论是真实的 Rust 程序仍然存在内存安全和并发缺陷。<sup>[15](https://dl.acm.org/doi/10.1145/3385412.3386036)</sup>

生态系统规模的扫描证实了这一模式。Rudra 在 6.5 小时内扫描了整个包含 43,000 个包的 crates.io 注册表，发现 264 个此前未知的内存安全缺陷，产生了 76 条 CVE 记录和 RustSec 数据库中的 112 条公告；截至 2021 年 9 月，这些占自 2016 年以来向 RustSec 报告的所有缺陷的 39.0% 和内存安全缺陷的 51.6%。<sup>[7](https://dl.acm.org/doi/pdf/10.1145/3477132.3483570)</sup> 因此，安全 Rust 是对特定类别缺陷的强有力保证，而不是对代码安全或无缺陷的保证。

## 性能与编译期权衡

与 C++ 的直接比较显示基准性能大致相当，两种语言都占据最高梯队；结果因工作负载和编译器后端而异。<sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup> 一项基于测量的 C++20 与稳定版 Rust 系统编程模式比较，涵盖 RAII、无锁数据结构、异步 I/O 和字符串处理，发现大多数场景下差异低于 5%。<sup>[8](https://github.laiyagushi.com/cschladetsch/CppRustComparison)</sup> 接受采访的工程师还报告说，用 Rust 重写 C 和 C++ 库通常能产生更快的代码，因为 Rust 更擅长内存布局。<sup>[16](https://blog.rust-lang.org/2025/12/19/what-do-people-love-about-rust/)</sup>

没有垃圾回收器带来了可预测的低延迟，没有暂停整个程序执行的停顿，这对实时系统、操作系统和游戏引擎很重要。<sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup>

**编译时间**是长期以来的抱怨。编译速度慢位居 2024 年 Rust 状况调查的生产力限制因素榜首，其次是调试支持不佳和编译器产物的高磁盘占用。<sup>[10](https://blog.rust-lang.org/2025/02/13/2024-State-Of-Rust-Survey-results/)</sup> 自 2023 年以来，通过并行编译和 Cranelift 后端，编译时间已改善约 40%，不过由于增量构建优化，C++ 在大型项目上仍然编译更快。<sup>[17](https://dev.to/code_2/rust-in-2025-did-it-finally-overtake-c-10ko)</sup>

## 与 C++、Go 和 Zig 的比较

Rust 的核心权衡是将内存和并发正确性检查移至编译期，在没有垃圾回收器的情况下提供强大的安全性，代价是更多的前期设计工作。其记录的痛点包括学习所有权和生命周期、比某些替代方案更长的编译时间、某些领域的生态系统空白、语言边界处的互操作复杂性，以及与 Go 的 goroutine 相比的异步复杂性。<sup>[11](https://softwarepatternslexicon.com/rust/appendices/comparing-rust-with-other-systems-programming-languages/)</sup> 具体针对 C++，Rust 在编译期拒绝那些在 C++ 中会成为未定义行为的代码，而运行时速度大致相当。<sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup><sup> • </sup><sup>[8](https://github.laiyagushi.com/cschladetsch/CppRustComparison)</sup>

## 生产环境中的采用

**Cloudflare** 在 Oxy 框架上用 Rust 将其代理重建为 FL2。内部指标显示，FL2 使用的 CPU 不到基于 Nginx/LuaJIT 的 FL1 的一半，内存也远不到一半，通过 FL2 提供服务的网站中位响应速度快 10 毫秒，性能提升 25%。新产品功能在 FL2 中 48 小时内即可上线，而在 FL1 中需要数周。<sup>[18](https://blog.cloudflare.com/20-percent-internet-upgrade/)</sup> Cloudflare 还将其机器人管理模块从 Lua 移植到 Rust，将 P50 延迟从 388 微秒降至 309 微秒（20%），P99 从 940 微秒降至 813 微秒（14%）。<sup>[19](https://blog.cloudflare.com/how-cloudflare-runs-ml-inference-in-microseconds/)</sup>

**PayPay** 报告称，从 Java 到 Rust 的重写将 CPU 从 1.5 个降至 0.15 个，降至原来的十分之一；从 Node.js 到 Rust 的重写将 CPU 从 40 个降至 2.4 个（16 倍），内存从 24 GB 降至 240 MB（100 倍），平均延迟下降近 30%。<sup>[20](https://blog.paypay.ne.jp/en/scaling-paypay-with-rust/)</sup> **Grab** 用 Rust 重写了一个 Golang 计数器服务，消耗的资源约为原来的 20%，节省了约 80%，并判断该 Rust 微服务的效率可能至少比其 Go 对应物高 50%。<sup>[14](https://engineering.grab.com/counter-service-how-we-rewrote-it-in-rust/)</sup>

在平台层面，微软使用 Rust 重写 Windows 的组件，谷歌用于 Android 和 Fuchsia，AWS 用于 [Firecracker](https://www.edgepedia.org/zh/firecracker) 等性能关键的基础设施服务。<sup>[2](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)</sup> 在 2024 年 Rust 状况调查中，38% 的受访者在工作中大部分编码时间使用 Rust，高于此前的 34%；45% 表示其组织对 Rust 有实质性的使用，上升了 7 个百分点；82% 报告 Rust 帮助其公司实现了目标。<sup>[10](https://blog.rust-lang.org/2025/02/13/2024-State-Of-Rust-Survey-results/)</sup>

## 数据一览

- 在截至 2025 年 5 月的 12 个月内，有 2,267,000 名开发者使用了 Rust；709,000 人将其列为主要语言。<sup>[4](https://blog.jetbrains.com/rust/2025/05/13/is-rust-the-future-of-programming/)</sup>
- 在从特定语言迁移到 Rust 的开发者中，C++ 是 2024 年被替换最多的语言，[Python](https://www.edgepedia.org/zh/python-programming-language)、C 和 Go 也较为显著。<sup>[4](https://blog.jetbrains.com/rust/2025/05/13/is-rust-the-future-of-programming/)</sup>
- Rust 在 2025 年 Stack Overflow 调查中以 72% 成为最受欣赏的语言，其次是 Gleam（70%）、[Elixir](https://www.edgepedia.org/zh/elixir-programming-language)（66%）和 Zig（64%）。<sup>[9](https://survey.stackoverflow.co/2025/technology)</sup>
- 2024 年，53% 的 Rust 用户报告每天或接近每天使用，上升了 4 个百分点；53% 认为自己高效，高于 2023 年的 47%，而只能编写简单程序的比例下降了 3 个百分点至 20%。<sup>[10](https://blog.rust-lang.org/2025/02/13/2024-State-Of-Rust-Survey-results/)</sup>
- 截至 2021 年 9 月 Rudra 研究时，crates.io 拥有 43,000 个包。<sup>[7](https://dl.acm.org/doi/pdf/10.1145/3477132.3483570)</sup>

## 近年来 Linux 内核中的 Rust

Rust 支持作为实验进入 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)，此后已逐步走向常规状态。2024 年 USENIX ATC 的一项研究在已合并和暂存的 Rust-for-Linux 代码中发现了 28 个缺陷，其中 18 个在 Linux 主线，10 个在 rust 分支；在已合并代码的缺陷中，12 个是编译缺陷，6 个与安全抽象相关，其中 8 个健全性缺陷破坏了内存安全，3 个破坏了线程安全。<sup>[21](https://www.usenix.org/system/files/atc24-li-hongyu.pdf)</sup> 在性能方面，研究发现 Rust 驱动总体上与 C 驱动相当，差距在 20% 以内，根据设置不同，吞吐量最多下降 61%、提升 67%；Rust e1000 网络驱动由于缺少优化，测得 ping 延迟比 C 驱动慢 11 倍。<sup>[21](https://www.usenix.org/system/files/atc24-li-hongyu.pdf)</sup> 完全实现的 Rust 驱动也比其 C 对应物更大：binder 为 1.2 倍，cpufreq_dt 为 2.1 倍，gpio 为 2.4 倍，semaphore 为 1.9 倍，且该项目缺乏主要驱动和文件系统，受制于缓慢的代码审查流程。<sup>[21](https://www.usenix.org/system/files/atc24-li-hongyu.pdf)</sup>

此后上游化加速。截至 2025 年 12 月，已合入上游的采用 Rust 的组件包括 AMCC QT2025 PHY 驱动、Android Binder 驱动、ASIX PHY 驱动（首个“Rust 参考驱动”）、DRM Panic 二维码生成器、Nova GPU 驱动、Null Block 驱动和 Tyr GPU 驱动。内核 6.12 至 6.17 添加了 KCFI、KASAN 和 SCS 支持、rbtree、ARMv7 支持、hrtimer、dma、KUnit 测试、XArray、工作队列延迟工作项以及一次重大的驱动核心更新。面向上游的驱动包括 Android ashmem、Apple AGX GPU 驱动和 NVMe 驱动。<sup>[22](https://lpc.events/event/19/contributions/2068/attachments/1859/3981/2025-12-11%20-%20LPC%202025%20-%20Rust%20for%20Linux.pdf)</sup>

与 Rust 项目的协调已成为常规：自 2024 年初以来，Rust 项目和 Rust for Linux 之间定期举行会议，后者一直是 Rust 项目的旗舰目标，2025 年下半年的目标聚焦于让 Rust for Linux 改用稳定版 Rust 编译器和稳定的语言特性。<sup>[23](https://lpc.events/event/20/contributions/2371/attachments/2229/4895/2026-10-06%20-%20LPC%202026%20-%20Rust%20for%20Linux.pdf)</sup> 内核目前使用 Rust 2021 版；迁移到 Rust 2024 将带来 let 链和不同的 drop 顺序等特性，但在较新版本语义下编写的修复在向后移植时可能破坏稳定内核。<sup>[23](https://lpc.events/event/20/contributions/2371/attachments/2229/4895/2026-10-06%20-%20LPC%202026%20-%20Rust%20for%20Linux.pdf)</sup> 随着 Linux 7.0 的发布，内核中的 Rust 实验正式结束，依据是 Rust 拉取请求中的文档更新，该请求还添加了 `__rust_helper` 标注，用于在内核 LTO 下改进 Rust 内核构建。<sup>[24](https://www.phoronix.com/news/Linux-7.0-Rust)</sup>

## 异步 Rust 与学习曲线

一项 Rust 项目定性研究发现，异步 Rust 被认为比同步 Rust 难得多，并指出了缺失的语言特性：trait 中的 async fn 前一年才可用且仍有缺口，工具支持也较弱。<sup>[16](https://blog.rust-lang.org/2025/12/19/what-do-people-love-about-rust/)</sup> 异步闭包原定随 Edition 2024 在 Rust 1.85 中稳定。<sup>[10](https://blog.rust-lang.org/2025/02/13/2024-State-Of-Rust-Survey-results/)</sup> Grab 的工程师同样发现异步 Rust 对习惯 Go 的开发者具有挑战性。<sup>[14](https://engineering.grab.com/counter-service-how-we-rewrote-it-in-rust/)</sup>

生态系统本身就是学习曲线的一部分。浏览 crates.io 对新手构成实际障碍，因为惯用的 Rust 依赖自定义 crate 进行错误处理和异步运行时，而 Rust 组织保持中立立场，不推荐入门套件。<sup>[16](https://blog.rust-lang.org/2025/12/19/what-do-people-love-about-rust/)</sup> 在入门时间方面，V100 报告称，来自 Node.js 或 Python 的工程师需要 4 到 6 周才能在 Rust 中高效工作，借用检查器是主要障碍，大约 3 个月后大多数工程师报告在 Rust 中比在 Node.js 中更高效。<sup>[25](https://v100.ai/blog/20-rust-microservices-zero-nodejs.html)</sup> 调查数据指向同一方向：自报生产力从 2023 年的 47% 上升到 2024 年的 53%。<sup>[10](https://blog.rust-lang.org/2025/02/13/2024-State-Of-Rust-Survey-results/)</sup>

## References

1. [What is Ownership? — The Rust Programming Language](https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html)
2. [Memory Safety in Systems Programming: A Comparative Analysis of Modern Alternatives to C/C++](https://uplatz.com/blog/memory-safety-in-systems-programming-a-comparative-analysis-of-modern-alternatives-to-c-c/)
3. [Introduction — The Rust Reference](https://doc.rust-lang.org/stable/reference/index.html)
4. [Is Rust the Future of Programming? — JetBrains Blog](https://blog.jetbrains.com/rust/2025/05/13/is-rust-the-future-of-programming/)
5. [References and Borrowing — The Rust Programming Language](https://doc.rust-lang.org/nightly/book/ch04-02-references-and-borrowing.html)
6. ['Against the Void': An Interview and Survey Study on How Rust Developers Use Unsafe Code](https://arxiv.org/html/2404.02230v2)
7. [Rudra: Finding Memory Safety Bugs in Rust at the Ecosystem Scale (CCS 2021)](https://dl.acm.org/doi/pdf/10.1145/3477132.3483570)
8. [CppRustComparison: Performance benchmarks C++ vs Rust](https://github.laiyagushi.com/cschladetsch/CppRustComparison)
9. [2025 Stack Overflow Developer Survey — Technology](https://survey.stackoverflow.co/2025/technology)
10. [2024 State of Rust Survey Results — Rust Blog](https://blog.rust-lang.org/2025/02/13/2024-State-Of-Rust-Survey-results/)
11. [Comparing Rust with Other Systems Programming Languages](https://softwarepatternslexicon.com/rust/appendices/comparing-rust-with-other-systems-programming-languages/)
12. [Rust VS C++ Comparison for 2026 — JetBrains Blog](https://blog.jetbrains.com/rust/2025/12/16/rust-vs-cpp-comparison-for-2026/)
13. [Validating References with Lifetimes — The Rust Programming Language](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)
14. [Counter Service: How we rewrote it in Rust — Grab Engineering](https://engineering.grab.com/counter-service-how-we-rewrote-it-in-rust/)
15. [Understanding memory and thread safety practices and issues in real-world Rust programs (PLDI 2020)](https://dl.acm.org/doi/10.1145/3385412.3386036)
16. [What do people love about Rust? — Rust Blog](https://blog.rust-lang.org/2025/12/19/what-do-people-love-about-rust/)
17. [Rust in 2025: Did It Finally Overtake C++? — dev.to](https://dev.to/code_2/rust-in-2025-did-it-finally-overtake-c-10ko)
18. [Cloudflare just got faster and more secure, powered by Rust](https://blog.cloudflare.com/20-percent-internet-upgrade/)
19. [How Cloudflare runs machine learning inference in microseconds](https://blog.cloudflare.com/how-cloudflare-runs-ml-inference-in-microseconds/)
20. [Scaling PayPay with Rust](https://blog.paypay.ne.jp/en/scaling-paypay-with-rust/)
21. [An Empirical Study of Rust-for-Linux (USENIX ATC 2024)](https://www.usenix.org/system/files/atc24-li-hongyu.pdf)
22. [LPC 2025 — Rust for Linux status slides](https://lpc.events/event/19/contributions/2068/attachments/1859/3981/2025-12-11%20-%20LPC%202025%20-%20Rust%20for%20Linux.pdf)
23. [LPC 2026 — Rust for Linux slides](https://lpc.events/event/20/contributions/2371/attachments/2229/4895/2026-10-06%20-%20LPC%202026%20-%20Rust%20for%20Linux.pdf)
24. [Linux 7.0 Officially Concluding The Rust Experiment (Phoronix)](https://www.phoronix.com/news/Linux-7.0-Rust)
25. [20 Rust Microservices, Zero Node.js: How V100 Rebuilt Its Stack](https://v100.ai/blog/20-rust-microservices-zero-nodejs.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/rust-programming-language

English original: https://www.edgepedia.org/rust-programming-language
