Rust 教程

Rust 是什么、它凭什么难、以及怎么学才不劝退。

Beginner
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

它凭什么难

ConceptWhy 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 代码

学习路线

  1. 装工具链 —— rustup、cargo、clippy
  2. 语法 —— 变量、类型、函数、流程控制
  3. 所有权、借用、生命周期 —— 这三章决定你能不能继续
  4. 抽象 —— 结构体、枚举、trait、泛型
  5. 进阶 —— 并发、异步、宏、智能指针
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.

Friends & Partners