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

レッスン 1: Rust の紹介 - パフォーマンスと安全性の両立

なぜ錆びるのか?ゼロコストの抽象化、GC を使用しないメモリの安全性。 Rust、Go、C++、Zig を比較してください。錆び、カーゴ、錆びアナライザーをインストールします。 Hello World、貨物プロジェクトの構造。

💻 プログラミング — レッスン 1 レッスン 1: Rust の紹介 - パフォーマンスの向上 安全性

Rust: 基本から上級まで

パート 1: Rust の基礎

xdev.asia

1. なぜ錆びるのか?

基準さび行くC++
メモリの安全性コンパイル時 (借用チェッカー)GCマニュアル
パフォーマンス水平C/C++良好 (GC 一時停止)最高
同時実行性恐れを知らない同時実行性ゴルーチンスレッド + ロック
コストゼロの抽象化✅❌✅
ヌルセーフティオプションなしnullptr

2.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 — パッケージマネージャーおよびビルドツール

# 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. ハローワールド

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

次の記事: 変数、型、制御フロー。