Rust is a systems programming language focused on three goals: performance, reliability, and productivity. With no garbage collector, Rust guarantees memory safety through a powerful type system and a unique ownership model.
Rust là ngôn ngữ lập trình hệ thống tập trung vào ba mục tiêu: hiệu suất, độ tin cậy và hiệu quả. Không có garbage collector, Rust đảm bảo an toàn bộ nhớ thông qua hệ thống kiểu dữ liệu mạnh mẽ và mô hình quyền sở hữu độc đáo.
Rust is suitable for many domains: systems programming, web development via WebAssembly, command-line tools, network services, and embedded systems. Created by Mozilla, it is now used by Microsoft, Google, Amazon, and many other major companies.
Rust phù hợp cho nhiều lĩnh vực: lập trình hệ thống, phát triển web với WebAssembly, công cụ dòng lệnh, dịch vụ mạng và lập trình nhúng. Được Mozilla tạo ra, Rust hiện được sử dụng bởi Microsoft, Google, Amazon và nhiều công ty lớn khác.
The recommended way to install Rust is via rustup — the official Rust toolchain installer. rustup lets you install, update, and manage multiple Rust versions simultaneously, and includes rustc (the compiler), cargo (the package manager), and other essential tools.
Cách được khuyến nghị để cài đặt Rust là thông qua rustup — công cụ quản lý toolchain Rust chính thức. rustup cho phép bạn cài đặt, cập nhật và quản lý nhiều phiên bản Rust cùng lúc, đồng thời bao gồm rustc (trình biên dịch), cargo (trình quản lý gói) và các công cụ quan trọng khác.
Open a terminal and run the following command. This script downloads and installs rustup along with the latest stable Rust toolchain. After it finishes, reload your shell configuration.
Mở terminal và chạy lệnh sau. Script này sẽ tải xuống và cài đặt rustup cùng với phiên bản Rust ổn định mới nhất. Sau khi hoàn tất, bạn cần tải lại cấu hình shell.
1curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh23# After installation, reload your shell:4source "$HOME/.cargo/env"On Windows, download the rustup-init.exe installer from https://rustup.rs and run it. You also need to install the Visual Studio C++ Build Tools for the Rust compiler to work.
Trên Windows, tải xuống trình cài đặt rustup-init.exe từ trang web chính thức tại https://rustup.rs và chạy nó. Bạn cũng cần cài đặt Visual Studio C++ Build Tools để trình biên dịch Rust hoạt động.
Trên Windows, Rust cần công cụ xây dựng C++ từ Visual Studio. Nếu bạn chưa có, trình cài đặt sẽ hướng dẫn bạn tải chúng. Chọn workload 'Desktop development with C++' trong Visual Studio Installer.
After installation completes, verify everything is working by checking the versions of the core tools. Open a new terminal and run the following commands.
Sau khi cài đặt hoàn tất, xác minh rằng mọi thứ hoạt động đúng bằng cách kiểm tra phiên bản của các công cụ cốt lõi. Mở terminal mới và chạy các lệnh sau.
1rustc --version2cargo --version3rustup --version45# Example output:6# rustc 1.78.0 (9b00956e5 2024-04-29)7# cargo 1.78.0 (54d8815d0 2024-03-26)8# rustup 1.27.0 (bbb9276d2 2024-03-08)910# Update to the latest stable Rust at any time:11rustup updateCargo is Rust's build system and package manager. It handles compiling your code, downloading and compiling library dependencies, and building distributable packages. To create a new project, use cargo new.
Cargo là công cụ xây dựng và quản lý gói của Rust. Nó xử lý việc biên dịch code, tải và biên dịch các thư viện phụ thuộc, và tạo các gói có thể phân phối. Để tạo dự án mới, dùng lệnh cargo new.
1# Create a new binary (executable) project2cargo new hello_world34# Create a new library project5cargo new my_library --lib67# Navigate into the project8cd hello_worldThe cargo new command creates a new directory with the following structure. Cargo.toml is the project configuration file and src/main.rs is the program entry point.
Lệnh cargo new tạo ra một thư mục mới với cấu trúc sau. Cargo.toml là file cấu hình dự án và src/main.rs là điểm vào của chương trình.
1hello_world/2├── Cargo.toml3└── src/4 └── main.rsWhen you create a new project with cargo new, the src/main.rs file already contains a Hello, World! program. This is the most basic Rust syntax and a good place to start understanding the language.
Khi bạn tạo dự án mới với cargo new, file src/main.rs đã chứa sẵn chương trình Hello, World! Đây là cú pháp Rust cơ bản nhất và là nơi tốt để bắt đầu hiểu ngôn ngữ.
fn main() {
println!("Hello, world!");
}- fn main() declares the main function — the entry point of every Rust executable. It takes no parameters and returns nothing.
fn main() khai báo hàm main — điểm vào của mọi chương trình Rust thực thi. Nó không nhận tham số và không trả về giá trị.
- println! is a macro (the ! indicates this, not a regular function) that prints text to stdout with a trailing newline.
println! là một macro (dấu ! cho biết điều đó, không phải hàm thông thường) in văn bản ra stdout kèm theo ký tự xuống dòng.
- Rust uses four spaces for indentation by convention, not tabs. rustfmt (the code formatter) enforces this automatically.
Rust dùng bốn dấu cách cho thụt lề theo quy ước, không phải tab. rustfmt (công cụ định dạng code) tự động thực thi điều này.
Cargo provides a set of commands to manage the Rust project lifecycle. These are the commands you will use every day during development.
Cargo cung cấp một tập hợp các lệnh để quản lý vòng đời dự án Rust. Đây là những lệnh bạn sẽ sử dụng hàng ngày trong quá trình phát triển.
1# Compile the project (debug build in target/debug/)2cargo build34# Compile and immediately run the program5cargo run67# Check for errors without producing a binary (much faster)8cargo check910# Build with full optimizations (release build in target/release/)11cargo build --release1213# Run tests14cargo test1516# Generate and open HTML documentation17cargo doc --open1819# Add a dependency to Cargo.toml20cargo add serde- cargo build: Compiles the project and creates a debug binary. The first build takes longer because dependencies are also compiled. Subsequent builds are faster due to caching.
cargo build: Biên dịch dự án, tạo binary gỡ lỗi. Lần đầu sẽ mất nhiều thời gian hơn vì các dependency cũng cần biên dịch. Các lần sau nhanh hơn nhờ bộ đệm.
- cargo run: Combines build and execution in one step. Very convenient during development.
cargo run: Kết hợp build và chạy chương trình trong một bước. Rất tiện lợi trong quá trình phát triển.
- cargo check: Only checks for errors without producing a binary — much faster than cargo build. Use it frequently to catch errors early while writing code.
cargo check: Chỉ kiểm tra lỗi mà không tạo binary, nhanh hơn nhiều so với cargo build. Dùng thường xuyên để bắt lỗi sớm trong khi viết code.
- cargo build --release: Produces a fully optimized production build. The binary runs significantly faster but compilation takes longer.
cargo build --release: Tạo bản dựng được tối ưu hóa đầy đủ cho sản xuất. Binary chạy nhanh hơn đáng kể nhưng quá trình biên dịch lâu hơn.
VS Code with the rust-analyzer extension is the most popular development environment for Rust. rust-analyzer provides code completion, type information, code navigation, refactoring, and real-time error diagnostics.
VS Code với extension rust-analyzer là môi trường phát triển phổ biến nhất cho Rust. rust-analyzer cung cấp tự động hoàn thành mã, thông tin kiểu dữ liệu, điều hướng code, tái cấu trúc và chẩn đoán lỗi theo thời gian thực.
- Install VS Code from https://code.visualstudio.com if you do not have it already.
Cài đặt VS Code từ https://code.visualstudio.com nếu bạn chưa có.
- Open Extensions (Ctrl+Shift+X), search for "rust-analyzer" and install it. This is the official extension maintained by the Rust team.
Mở Extensions (Ctrl+Shift+X), tìm kiếm 'rust-analyzer' và cài đặt. Đây là extension chính thức được nhóm Rust duy trì.
- Optional: Install the "Even Better TOML" extension for better Cargo.toml syntax support with autocompletion and validation.
Tùy chọn: Cài đặt extension 'Even Better TOML' để hỗ trợ cú pháp Cargo.toml tốt hơn với tự động hoàn thành và validation.
- Optional: Install the "CodeLLDB" extension for debugging support with breakpoints and variable inspection.
Tùy chọn: Cài đặt extension 'CodeLLDB' để hỗ trợ debugging với breakpoint và xem biến.
rust-analyzer sẽ tự động tải xuống các thành phần cần thiết khi bạn mở dự án Rust lần đầu. Quá trình này có thể mất vài phút tùy thuộc vào tốc độ kết nối internet.
Cargo.toml is the Rust project configuration file, written in TOML format. It contains project metadata and the list of libraries (crates) the project depends on.
Cargo.toml là file cấu hình dự án Rust, được viết theo định dạng TOML. Nó chứa metadata về dự án và danh sách các thư viện (crates) mà dự án phụ thuộc vào.
1[package]2name = "hello_world"3version = "0.1.0"4edition = "2021"56[dependencies]7# Add external crates from crates.io here8serde = { version = "1", features = ["derive"] }9tokio = { version = "1", features = ["full"] }1011[dev-dependencies]12# Dependencies only used in tests — not included in production builds13criterion = "0.5"1415[profile.release]16opt-level = 3 # Maximum optimization17lto = true # Link-time optimization- [package]: Contains project metadata. edition = "2021" is the recommended Rust edition, providing the most modern syntax and features.
[package]: Phần này chứa metadata của dự án. edition = '2021' là phiên bản Rust được khuyến nghị, mang lại cú pháp và tính năng hiện đại nhất.
- [dependencies]: Lists the external crates the project needs. Cargo automatically downloads and compiles them from crates.io when you run cargo build.
[dependencies]: Liệt kê các crate bên ngoài mà dự án cần. Cargo sẽ tự động tải và biên dịch chúng từ crates.io khi bạn chạy cargo build.
- [dev-dependencies]: Dependencies only used during development and testing, not included in production builds.
[dev-dependencies]: Các dependency chỉ dùng trong môi trường phát triển và kiểm thử, không được đưa vào bản dựng sản xuất.
crates.io là registry trung tâm của hệ sinh thái Rust với hàng chục nghìn thư viện mã nguồn mở. Tài liệu cho tất cả các crate được tự động tạo và phát hành tại docs.rs.
Key Takeaways
Điểm Chính
- Rust is installed via rustup, which manages the toolchainRust được cài đặt thông qua rustup, công cụ quản lý toolchain
- Cargo is Rust's build system and package managerCargo là hệ thống build và trình quản lý gói của Rust
- cargo run compiles and runs your program in one stepcargo run biên dịch và chạy chương trình trong một bước
- Cargo.toml defines project metadata and dependenciesCargo.toml định nghĩa metadata dự án và các dependency
Practice
Test your understanding of this chapter
What is Rust's official build system and package manager?
Hệ thống build và trình quản lý gói chính thức của Rust là gì?
Rust source files use the .rs file extension.
File mã nguồn Rust sử dụng phần mở rộng .rs.
Complete the cargo command to compile and run a Rust project
Hoàn thành lệnh cargo để biên dịch và chạy dự án Rust
cargo Which file contains the project metadata and dependencies in a Rust project?
File nào chứa metadata dự án và các dependency trong dự án Rust?
The cargo check command produces a compiled binary in the target/ folder.
Lệnh cargo check tạo ra một binary biên dịch trong thư mục target/.