Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Cargo Commands

คำสั่ง Cargo ที่ใช้บ่อย


🚀 พื้นฐาน

คำสั่งคำอธิบาย
cargo new project_nameสร้างโปรเจกต์ใหม่ (binary)
cargo new --lib lib_nameสร้างโปรเจกต์ใหม่ (library)
cargo initสร้างโปรเจกต์ในโฟลเดอร์ปัจจุบัน
cargo buildBuild โปรเจกต์ (debug)
cargo build --releaseBuild โปรเจกต์ (release/optimized)
cargo runBuild และ Run
cargo run --releaseRun แบบ release

🧪 Testing

คำสั่งคำอธิบาย
cargo testรัน tests ทั้งหมด
cargo test test_nameรัน test ที่ชื่อตรง
cargo test -- --show-outputแสดง println!
cargo test -- --test-threads=1รัน sequential
cargo test -- --ignoredรัน ignored tests
cargo test --docรัน doc tests

📦 Dependencies

คำสั่งคำอธิบาย
cargo add crate_nameเพิ่ม dependency
cargo add tokio --features fullเพิ่มพร้อม features
cargo remove crate_nameลบ dependency
cargo updateอัปเดต dependencies
cargo treeแสดง dependency tree

📋 ตรวจสอบ

คำสั่งคำอธิบาย
cargo checkCheck ว่า compile ได้ (เร็วกว่า build)
cargo clippyLint หา improvements
cargo fmtFormat code
cargo fmt --checkCheck format
cargo auditตรวจ security vulnerabilities

📖 Documentation

คำสั่งคำอธิบาย
cargo docสร้าง documentation
cargo doc --openสร้างและเปิดใน browser
cargo doc --no-depsไม่รวม dependencies

🔧 Advanced

คำสั่งคำอธิบาย
cargo cleanลบ target directory
cargo publishPublish ไป crates.io
cargo install crate_nameติดตั้ง binary
cargo uninstall crate_nameลบ binary
cargo benchRun benchmarks

🎯 Examples

# สร้างโปรเจกต์ใหม่
cargo new my_project
cd my_project

# เพิ่ม dependencies
cargo add serde --features derive
cargo add tokio --features full

# Build และ Run
cargo run

# ตรวจสอบ code
cargo check
cargo clippy
cargo fmt

# รัน tests
cargo test

# Build สำหรับ production
cargo build --release

# สร้าง documentation
cargo doc --open

📁 Examples

# รัน example
cargo run --example example_name

# รัน specific binary
cargo run --bin binary_name

# รัน workspace member
cargo run -p package_name

🛠️ Workspace

# Cargo.toml (root)
[workspace]
members = [
    "crate1",
    "crate2",
]

# Build all
cargo build --workspace

# Test all
cargo test --workspace

# Run specific
cargo run -p crate1

🔍 Useful Flags

Flagคำอธิบาย
--verbose หรือ -vแสดงรายละเอียด
--quiet หรือ -qลดการแสดงผล
--jobs N หรือ -j Nจำนวน parallel jobs
--target tripleCross compile
--features "f1 f2"เปิด features
--all-featuresเปิดทุก features
--no-default-featuresปิด default features

👉 Resources