คำสั่ง 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
คำสั่ง คำอธิบาย
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
คำสั่ง คำอธิบาย
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
คำสั่ง คำอธิบาย
cargo docสร้าง documentation
cargo doc --openสร้างและเปิดใน browser
cargo doc --no-depsไม่รวม dependencies
คำสั่ง คำอธิบาย
cargo cleanลบ target directory
cargo publishPublish ไป crates.io
cargo install crate_nameติดตั้ง binary
cargo uninstall crate_nameลบ binary
cargo benchRun benchmarks
# สร้างโปรเจกต์ใหม่
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
# รัน example
cargo run --example example_name
# รัน specific binary
cargo run --bin binary_name
# รัน workspace member
cargo run -p package_name
# Cargo.toml (root)
[workspace]
members = [
"crate1",
"crate2",
]
# Build all
cargo build --workspace
# Test all
cargo test --workspace
# Run specific
cargo run -p crate1
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