Rust 教程
Rust 是什么、它凭什么难、以及怎么学才不劝退。
Key PointsRust 用编译期的所有权与借用检查,换来运行期的内存安全与零 GC。
Rust 是什么
Rust is a systems language started at Mozilla, reaching 1.0 in 2015. Its goal: C/C++ performance and memory control with memory safety. The trick is moving memory management from a runtime GC into compile-time checks.
Rust
fn main() {
let s = String::from("你好,Rust");
println!("{}", s);
}Output
你好,Rust它凭什么难
| Concept | Why it hurts |
|---|---|
| 所有权 | 每个值只有一个主人,赋值即移动 |
| 借用 | 同一时刻只能有一个可变借用 |
| 生命周期 | 编译器要你标注引用的存活范围 |
| 无 GC | 没有运行时兜底,全在编译期算清 |
The hard part is not syntax but mindset. Coming from Java or Python you are used to passing references freely; Rust keeps replying "that value is already borrowed". The first two weeks are the hump.
能做什么
- 系统工具 —— ripgrep、fd、bat、hyperfine
- Web 后端 —— Axum、Actix Web、Rocket
- WASM —— 前端性能敏感模块
- 嵌入式 —— 无标准库的裸机开发
- 基础设施 —— Linux 内核已接受 Rust 代码
学习路线
- 装工具链 —— rustup、cargo、clippy
- 语法 —— 变量、类型、函数、流程控制
- 所有权、借用、生命周期 —— 这三章决定你能不能继续
- 抽象 —— 结构体、枚举、trait、泛型
- 进阶 —— 并发、异步、宏、智能指针
Do not start with a big project. Rust's error messages are excellent — following the compiler's suggestion is the fastest way to learn. It often says "try
&mut" outright.Rust 是什么
Rust 是 Mozilla 发起、2015 年发布 1.0 的系统级语言。它想解决的问题是:既要 C/C++ 的性能和内存控制,又要内存安全。做法是把内存管理从「运行时 GC」搬到「编译期检查」。
Rust
fn main() {
let s = String::from("你好,Rust");
println!("{}", s);
}Output
你好,Rust它凭什么难
| 概念 | 为什么难 |
|---|---|
| 所有权 | 每个值只有一个主人,赋值即移动 |
| 借用 | 同一时刻只能有一个可变借用 |
| 生命周期 | 编译器要你标注引用的存活范围 |
| 无 GC | 没有运行时兜底,全在编译期算清 |
难点不在语法,在思维方式。写惯 Java/Python 的人习惯「随便传引用」,Rust 会一次次告诉你「这个值已经被借走了」。熬过前两周就好了。
能做什么
- 系统工具 —— ripgrep、fd、bat、hyperfine
- Web 后端 —— Axum、Actix Web、Rocket
- WASM —— 前端性能敏感模块
- 嵌入式 —— 无标准库的裸机开发
- 基础设施 —— Linux 内核已接受 Rust 代码
学习路线
- 装工具链 —— rustup、cargo、clippy
- 语法 —— 变量、类型、函数、流程控制
- 所有权、借用、生命周期 —— 这三章决定你能不能继续
- 抽象 —— 结构体、枚举、trait、泛型
- 进阶 —— 并发、异步、宏、智能指针
别一上手就写大项目。Rust 的错误信息质量极高,照着编译器提示改是最快的学法。它经常直接告诉你「应该改成
&mut」。