Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorSteve Klabnik, Carol Nichols, Contributions from the Rust Community, [译] Rust 中文翻译项目组

Rust 程序设计语言 中文 也译为 Rust 权威指南,是 Rust 官方推出的学习 Rust 的必备教程。Rust Wiki 版的 Rust 程序设计语言简体中文版将由 Rust 中文翻译项目组持续维护和更新,确保内容最新最全。

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 The official Rust programming language guide, translated into Chinese, takes you from installation and basic syntax through ownership, generics, testing, and building real projects—essential for any developer serious about learning Rust's safety and performance. 【Book Arc】 - **Opening (~0%–4%)**: Covers installation via rustup, verifying with `rustc --version`, and writing your first "Hello, world!" program. Introduces Cargo for project management and builds a simple guessing game to demonstrate `let`, `match`, functions, and external crates like `rand`. - **Early (~4%–17%)**: Dives into fundamental concepts—variables, data types (integers, floats), functions, and control flow. Explores ownership, borrowing, references, and slices, which are Rust's unique memory safety features. Introduces structs, methods, and enums like `Option<T>`. - **Early (~17%–25%)**: Covers the module system for organizing code, including paths, privacy rules, and splitting modules into files. Discusses common collections: `String` (UTF-8 handling), vectors, and `HashMap` with the `entry` API. - **Middle (~25%–42%)**: Focuses on error handling with `Result` and `panic!`, then generics, traits, and lifetimes—key for writing flexible, reusable code. Introduces testing with `cargo test`, `assert_eq!`, and unit/integration test patterns. - **Middle (~42%–54%)**: Builds a complete I/O project (minigrep) applying previous concepts: parsing command-line arguments, refactoring with structs, using TDD for search logic, and handling environment variables. Refines the project with iterators and closures, and covers Cargo workspaces and documentation with `cargo doc`. 【Key Takeaways】 - **Ownership is Rust's core innovation** (Early): The rules of ownership, borrowing, and references prevent memory bugs at compile time. Understanding this is non-negotiable for writing idiomatic Rust. - **Slices provide safe, efficient access to data** (Early): Returning `&str` slices instead of indices prevents logic errors like using invalid indexes after data changes. The compiler enforces slice validity. - **`Option<T>` eliminates null pointer errors** (Early): By forcing explicit handling of `Some` and `None`, Rust removes entire classes of bugs common in other languages. This design choice increases code confidence. - **The module system scales code organization** (Early): Using `mod`, `pub`, and file-based modules keeps projects maintainable. Paths (absolute and relative) and privacy rules are straightforward once practiced. - **Generics, traits, and lifetimes work together** (Middle): These features enable flexible code without runtime cost. Trait bounds ensure types have required behavior, while lifetimes prevent dangling references—all checked at compile time. - **Testing is built-in and essential** (Middle): `cargo test` with `assert_eq!` and `assert_ne!` catches bugs early. Unit tests verify private functions; integration tests check public APIs. Even with Rust's safety, tests reduce logic errors. - **Iterators and closures improve code efficiency** (Middle): Using `env::args` as an iterator and methods like `next()` avoids unnecessary cloning and indexing, making code more idiomatic and performant. - **Documentation is a first-class citizen** (Middle): `cargo doc` generates HTML from `///` comments. Sections like `# Examples`, `Panics`, and `Errors` help users understand and use your API correctly. 【Reading Tips】 - **Skim the installation and setup sections** (0%–4%) if you're experienced; focus instead on the guessing game to see real Rust patterns in action. - **Deep-read the ownership chapter** (Early): It's the hardest and most important part. Work through the examples and error messages carefully—they teach you how the borrow checker thinks. - **Practice the minigrep project** (Middle): This is where everything clicks. Follow the TDD steps and refactoring process to see how a real Rust project evolves. - **Pay attention to compiler errors**: The book shows many error messages deliberately. Reading them teaches you to diagnose and fix issues on your own. - **Use the local docs**: Run `rustup doc` and `cargo doc --open` to explore standard library types and traits mentioned throughout—this builds self-sufficiency. 【Coverage Limits】 This guide covers the book's progression from basics through ownership, collections, error handling, generics, testing, and a complete I/O project. Excerpts do not cover advanced topics like trait objects (Chapter 17), complex lifetimes (Chapter 19), concurrency, or unsafe Rust.
Excerpt 1
提供的⽂档,并在浏览器中打 本项⽬通过动⼿实践,向你介绍了许多 Rust 的新概念: let、 match、函数、使⽤ 外部 crate,等等。接下来的⼏章,你会继续深⼊学习这些概念。第 3 章介绍⼤部分 编程语⾔都有的概念,⽐如变量、数据类型和函数,以及如何在 Rust 中使⽤它们。 第 4 章探索所有权(ow...
View in text
Excerpt 2
me specifier --> src/main.rs:4:12 | 4 | email: &str, | ^ expected named lifetime parameter | help: consider introducing a named lifetime parameter | 1 ~ stru...
View in text
Excerpt 3
expected type `{integer}` found type `{float}` 如果想要定义⼀个 x 和 y 可以有不同类型且仍然是泛型的 Point 结构体,我们可 以使⽤多个泛型类型参数。在⽰例 10-8 中,我们修改 Point 的定义为拥有两个泛型 类型 T 和 U。其中字段 x 是 T 类...
View in text
Excerpt 4
更 好⼀些。随着你对 Rust 更加熟练,将能更轻松的直奔合适的⽅法,不过现在调 ⽤ clone 是完全可以接受的。 我们更新 main 将 parse_config 返回的 Config 实例放⼊变量 config 中,并将之 前分别使⽤ query 和 filename 变量的代码更新为现在的使⽤ Confi...
View in text
Excerpt 5
* 操作符⽆限递归替换,解 引⽤出上⾯ i32 类型的值就停⽌了,这个值与⽰例 15-9 中 assert_eq! 的 5 相匹 配。 函数和⽅法的隐式解引⽤强制转换 解引⽤强制转换(deref coercions)是 Rust 在函数或⽅法传参上的⼀种便利。解引 ⽤强制转换只能⼯作在实现了 Deref trai...
View in text
Excerpt 6
_eq!("", post.content()); post.approve(); assert_eq!("I ate a salad for lunch today", post.content()); } ⽰例 17-11: 展⽰了 blog crate 期望⾏为的代码 我们希望允许⽤⼾使⽤ Post::ne...
View in text
Excerpt 7
; // --snip-- fn handle_connection(mut stream: TcpStream) { let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let contents = fs::read_to_string(...
View in text
Excerpt 8
ent! ! {...} , 宏展开 ident![...] 按位⾮或逻 ! !expr Not 辑⾮ != var != expr 不等⽐较 PartialEq % expr % expr 算术取模 Rem 算术取模与 %= var %= expr RemAssign 赋值 & &expr , &mut exp...
View in text
Tags
AI categories
Programming LanguageRustBackend
Publish Year: 2022
Language: English
File Format: PDF
File Size: 13.5 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…