Chuyển đến nội dung chính

Lesson 1: Introducing Rust - Performance meets Safety

Why Rust? Zero-cost abstractions, memory safety without GC. Compare Rust vs Go vs C++ vs Zig. Install rustup, cargo, rust-analyzer. Hello World, cargo project structure.

💻 Programming — Lesson 1 Lesson 1: Introducing Rust - Performance meets Safety

Rust: From Basics to Advanced

Part 1: Rust Fundamentals

xdev.asia

1. Why Rust?

CriteriaRustGoC++
Memory safetyCompile-time (borrow checker)GCManual
PerformanceHorizontal C/C++Good (GC pause)Highest
ConcurrencyFearless concurrencyGoroutinesThreads + locks
Zero-cost abstractions✅❌✅
Null safetyOptionnilnullptr

2. Install 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

Next article: Variables, Types & Control Flow.