Ngôn ngữ lập trình Rust
Một hướng dẫn giới thiệu toàn diện về ngôn ngữ lập trình Rust, bao gồm các khái niệm lập trình hệ thống, an toàn bộ nhớ, đồng thời thực thi và công cụ Rust (Cargo, rustup). Nội dung chuyển từ cú pháp cơ bản đến xây dựng các dự án hoàn chỉnh như một máy chủ web đa luồng.
Tổng quan khóa học
📚 Tóm tắt nội dung
Một hướng dẫn giới thiệu toàn diện về Rust, bao gồm các khái niệm lập trình hệ thống, an toàn về bộ nhớ, đồng thời thực hiện và công cụ Rust (Cargo, rustup). Nội dung chuyển từ cú pháp cơ bản đến việc xây dựng các dự án hoàn chỉnh như một máy chủ web đa luồng.
Thành thạo nghệ thuật lập trình hệ thống an toàn, nhanh chóng và đồng thời với hướng dẫn toàn diện về Rust.
Tác giả: Steve Klabnik và Carol Nichols, với sự đóng góp từ cộng đồng Rust
Lời cảm ơn: Những đóng góp từ cộng đồng Rust; được xuất bản dưới dạng sách in và ebook từ No Starch Press.
🎯 Mục tiêu học tập
- Cài đặt và quản lý các công cụ Rust trên nhiều hệ điều hành bằng
rustup. - Viết, biên dịch và chạy một chương trình Rust cơ bản, xác định các thành phần cốt lõi trong mã nguồn.
- Sử dụng Cargo để tạo cấu trúc dự án chuẩn, quản lý quá trình biên dịch và sản xuất các tệp nhị phân tối ưu cho bản phát hành.
- Thu thập và lưu trữ đầu vào người dùng trong khi quản lý tính thay đổi của biến và các lỗi I/O tiềm ẩn.
- Tích hợp các phụ thuộc bên ngoài bằng Cargo và quản lý các bản dựng tái tạo được thông qua
Cargo.lock. - Triển khai logic trò chơi bằng cách sử dụng suy diễn kiểu, phân tích chuỗi, vòng lặp và toán tử kiểm soát luồng
match. - Phân biệt giữa hằng số và ghi đè biến để quản lý tính bất biến dữ liệu và phạm vi.
- Triển khai chính xác các kiểu dữ liệu nguyên thủy (số nguyên, số thực, boolean, ký tự) và kiểu dữ liệu kết hợp (tuple, mảng).
- Phân biệt giữa câu lệnh và biểu thức để định nghĩa hàm với giá trị trả về cụ thể.
- Phân biệt giữa bộ nhớ ngăn xếp và bộ nhớ đệm, và giải thích cách Rust quản lý dữ liệu trên đệm.
Bài học 共 21 课时 · 预计 63.0h
Bài học
Lesson
This lesson introduces the Rust programming language, focusing on its core philosophy of developer empowerment, memory safety, and high-performance systems programming. Students will learn to navigate the Rust ecosystem, including the Cargo package manager, the six-week release cycle, and the importance of accessible documentation.
This lesson introduces interactive programming in Rust by teaching students how to handle console input and output using the standard library and macros. You will learn to manage program state through variables and mutability while implementing a basic guessing game that processes user input.
This lesson covers fundamental programming concepts in Rust, focusing on the use of constants for memory safety and performance through compile-time evaluation. Students will learn how to declare constants with explicit type annotations and distinguish them from mutable variables to ensure code reliability.
This lesson explores how Rust achieves memory safety and high performance by replacing manual memory management and garbage collection with a compile-time ownership system. Students will learn how the borrow checker enforces strict rules to prevent common memory bugs while ensuring efficient, predictable execution.
This lesson explores the transition from handling raw data using slices and arrays to organizing information into defined structures. You will learn how to master struct syntax, manage member access, and improve code clarity by moving beyond anonymous memory types to domain-specific schemas.
This lesson explores the power of Rust enums, focusing on how they provide type safety, namespacing, and exhaustive state management through pattern matching. Students will learn to define multi-variant enums with associated data and implement robust logic using match expressions to handle various program states.
This lesson explores Rust's hierarchical module system, teaching students how to organize code into packages, crates, and modules to improve scalability and maintainability. You will learn how to manage item visibility with the `pub` keyword and structure projects using a dual-crate architecture to separate core logic from executable interfaces.
This lesson explores how Rust manages heap-allocated collections like vectors and strings, focusing on dynamic memory growth and the role of the Drop trait in RAII-based memory safety. Students will also learn to navigate Rust's module system, including file resolution rules and idiomatic path management using pub use.
This lesson explores Rust's philosophy of reliability by distinguishing between recoverable errors, which are managed via the Result type, and unrecoverable errors, which trigger a panic to ensure system integrity. Students will learn to implement the fail-fast principle and design robust recovery mechanisms to prevent silent data corruption and maintain predictable application states.
This lesson explores the path to robust abstraction in Rust by demonstrating how to refactor duplicate code into concrete functions before transitioning to generic types. Students will learn to identify logic duplication, implement traits for shared behavior, and utilize lifetime annotations to manage complex ownership scenarios.
This lesson explores the role of automated testing in Rust, highlighting how the #[test] attribute and the cargo test toolchain complement the compiler to ensure functional correctness. Students will learn the standard test lifecycle—setup, execution, and assertion—while mastering techniques like conditional compilation and documentation testing to build robust, reliable software.
This lesson explores how to build a robust command-line tool in Rust by separating core logic into a library crate and keeping the binary crate minimal. Students will learn to implement modular project structures, manage command-line arguments, and utilize Rust’s testing framework to ensure code reliability.
This lesson explores Rust's functional programming features, focusing on how closures capture their environment using the Fn, FnMut, and FnOnce traits. Students will learn to implement efficient, zero-cost data pipelines by utilizing lazy iterator adaptors and consumers to replace manual loops.
This lesson covers advanced Cargo project management, including the use of workspaces, custom build logic, and the Crates.io publishing workflow. Students will also learn how to optimize Rust applications by configuring release profiles and feature flags to balance compilation speed with runtime performance.
This lesson explores the Rust smart pointer pattern, which uses structs to encapsulate memory addresses with metadata and custom logic for ownership and cleanup. Students will learn how to implement the Deref and Drop traits to manage heap-allocated data safely and effectively.
This lesson explores Rust's "Fearless Concurrency" philosophy, which leverages the ownership model and type system to detect data races at compile-time rather than runtime. Students will learn to implement safe shared state using primitives like Arc and Mutex while mastering the Send and Sync marker traits to ensure thread safety.
This lesson explores how Rust implements object-oriented principles like encapsulation and polymorphism by favoring composition and traits over traditional class-based inheritance. Students will learn to leverage the State-as-Type pattern to enforce logic and safety at compile-time, ensuring robust and maintainable systems programming.
This lesson explores the fundamental role of pattern matching in Rust, demonstrating how patterns function as the core mechanism for variable binding, function parameters, and data destructuring. Students will learn to distinguish between irrefutable and refutable patterns to ensure code reliability and master the use of advanced structural decomposition techniques.
This lesson explores the necessity of Unsafe Rust as a controlled mechanism to bypass the compiler's conservative static analysis when interacting with hardware or complex memory operations. Students will learn to implement safe abstractions by wrapping unsafe code, mastering raw pointers, and understanding the trade-offs between compile-time safety and performance.
This lesson explores building a high-performance web server in Rust by leveraging procedural macros for compile-time routing and efficient request handling. Students will learn to manage the TcpStream lifecycle and utilize synchronization primitives to ensure robust, thread-safe communication.
This lesson explores the lexical architecture of Rust, focusing on the use of specialized literals, documentation comments, and structural symbols. Students will learn to navigate the Rust reference to effectively utilize raw strings, numeric suffixes, and diverging types to write more precise and maintainable code.