1. Tại sao Rust?
| Tiêu chí | Rust | Go | C++ |
|---|---|---|---|
| Memory safety | Compile-time (borrow checker) | GC | Manual |
| Performance | Ngang C/C++ | Tốt (GC pause) | Cao nhất |
| Concurrency | Fearless concurrency | Goroutines | Threads + locks |
| Zero-cost abstractions | ✅ | ❌ | ✅ |
| Null safety | Option<T> | nil | nullptr |
2. Cài đặt Rust
# Cài rustup (Rust toolchain manager)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Verify
rustc --version
cargo --version
# Update
rustup update
# Components hữu ích
rustup component add clippy # Linter
rustup component add rustfmt # Formatter
3. Cargo — Package Manager & Build Tool
# Tạo project mới
cargo new my-app
cd my-app
# Cấu trúc project
# my-app/
# ├── Cargo.toml # Manifest (giống package.json)
# ├── src/
# │ └── main.rs # Entry point
# Cargo.toml
[package]
name = "my-app"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
4. Hello World
fn main() {
let name = "Rust";
println!("Hello, {}!", name);
// Formatted printing
println!("Decimal: {}", 42);
println!("Binary: {:b}", 42);
println!("Debug: {:?}", vec![1, 2, 3]);
println!("Pretty: {:#?}", vec![1, 2, 3]);
}
# Build & Run
cargo run
# Build release
cargo build --release
# Check (compile without codegen — nhanh hơn)
cargo check
# Format code
cargo fmt
# Lint
cargo clippy
Bài tiếp theo: Variables, Types & Control Flow.