AI guide
# Rust 程序设计语言 简体中文版 (The Rust Programming Language, 2e)
## 【One-Line Pitch】
The definitive, community-maintained guide to Rust, taking you from your first `Hello, world!` through ownership, traits, and building real projects — essential reading for any programmer serious about systems programming, whether you're coming from C, Python, or JavaScript.
## 【Book Arc】
- **Opening (~0%–10%)**: Installation, Cargo basics (`new`, `build`, `run`, `check`), and a complete "Guessing Game" project that introduces variables, input/output, and `match` — a fast, hands-on on-ramp that shows Rust's workflow before diving into theory.
- **Early (~10%–23%)**: Core language fundamentals — variables and mutability, control flow, and the pivotal ownership system (stack vs. heap, borrowing, slices) — followed by structs and enums, establishing the mental model that makes Rust unique.
- **Early-to-Middle (~23%–32%)**: Modules and privacy for organizing crates, plus collections (String, Vec, HashMap) with their pitfalls, and error handling — `panic!`, `Result`, and the `?` operator — teaching you to write robust, production-ready code.
- **Middle (~39%–48%)**: Generics, traits, and lifetimes — the abstraction toolkit — followed by a thorough testing chapter covering unit tests, integration tests, and test organization, showing how to verify behavior as you build.
- **Late (~48% onward)**: A capstone project (building a `grep`-like tool) that synthesizes everything learned, demonstrating how to structure a real binary crate with `src/main.rs` and `src/lib.rs` separation for testability.
## 【Key Takeaways】
- **Cargo is your constant companion** (Opening): `cargo new`, `build`, `run`, and `check` form a fast feedback loop; `cargo check` skips binary generation for quicker compile validation, and `cargo doc --open` gives you local dependency docs. This workflow is identical across all operating systems.
- **Ownership is Rust's third way to manage memory** (Early): Unlike garbage collection or manual allocation, Rust uses compile-time rules — values have a single owner, and when that owner goes out of scope, memory is freed. This gives you safety without runtime overhead.
- **Stack vs. heap determines behavior** (Early): Stack data needs known, fixed sizes; heap data uses pointers and requires allocation. Understanding this distinction explains why Rust makes the design choices it does — pushing to the stack is fast, while heap access requires pointer indirection.
- **Borrowing rules prevent dangling references** (Early): At any time, you can have either one mutable reference or multiple immutable references — never both. References must always be valid, which the compiler enforces, eliminating an entire class of memory bugs.
- **`match` is exhaustive by design** (Early): Rust's compiler checks that all possible cases are covered — for example, `Option<T>` forces you to handle `None`. This turns a billion-dollar class of null-pointer bugs into a compile-time error.
- **Traits enable abstraction with coherence** (Middle): You can implement your traits for any type, but the orphan rule prevents implementing external traits on external types — ensuring other people's code can't break yours, and vice versa.
- **Lifetimes describe relationships, not durations** (Middle): Lifetime annotations like `'a` don't change how long references live; they tell the borrow checker how multiple references relate, enabling it to verify your code's validity.
- **Testing is built into the workflow** (Middle): `#[test]` attributes, `cargo test`, unit tests in modules, and integration tests in a `tests/` directory — with the key insight that binary crates need a `src/lib.rs` companion to be testable from integration tests.
## 【Reading Tips】
- **Skim the Guessing Game chapter** (~3%–10%): It's a great warm-up, but don't get bogged down — the real meat starts with ownership. Come back to it if you need a refresher on Cargo basics.
- **Deep-read the ownership chapter** (~10%–13%): This is the hardest conceptual leap for most programmers. Read it slowly, work through the examples, and expect to re-read it. The stack/heap analogy (restaurant seating) is worth internalizing.
- **Pay attention to compiler errors**: The book shows real error messages throughout — these are teaching tools. Reading them carefully will train you to understand Rust's diagnostics, which are among the best in any language.
- **Do the exercises, don't just read**: The book builds projects incrementally (Guessing Game, then a grep clone). Type the code yourself, break it, and fix it — that's where the learning happens.
- **Use the testing chapter as a reference** (~42%–48%): You don't need to master every testing feature upfront, but know it's there. The distinction between unit tests (private internals) and integration tests (public API) is a key insight for structuring projects.
## 【Coverage Limits】
This guide covers the book's first half (through ~48%), including fundamentals, ownership, structs/enums, modules, collections, error handling, generics/traits/lifetimes, and testing. The excerpts do not cover the later project chapters, concurrency, smart pointers, or advanced features — those require reading the book directly.
##
Passage locations
Page 20
Cargo 内容: • 可以使用 cargo new 创建项目。 • 可以使用 cargo build 构建项目。 • 可以使用 cargo run 一步构建并运行项目。 • 可以使用 cargo check 在不生成二进制文件的情况下构建项目来检查错误。 • 有别于将构建结果放在与源码相同的目录,Cargo 会...
View in text
Excerpt 2
rait `std::fmt::Display` is not implemented for `Rectangle` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty- print) instead 让我们来...
View in text
Excerpt 3
`dev` profile [unoptimized + debuginfo] target(s) in 0.27s Running `target/debug/panic` thread 'main' panicked at src/main.rs:4:6: index out of bounds: the l...
View in text
Excerpt 4
result = add(2, 2); assert_eq!(result, 4); } } 示例 11-1:由 cargo new 自动生成的测试模块和函数 文件以一个示例 add 函数开头,这样我们就有东西可以测试。 220/562 Rust 程序设计语言 简体中文版 assert_eq!(add_two(2...
View in text