ข้ามไปยังเนื้อหา

Tooling, Testing & Deployment

ใน Node.js คุณต้องประกอบ toolchain เองทั้งหมด: npm (หรือ yarn/pnpm) สำหรับ packages, tsc สำหรับ compile, eslint สำหรับ lint, prettier สำหรับ format, jest/vitest สำหรับ test และ esbuild/webpack/rollup สำหรับ bundle แต่ละตัวมี config file ของตัวเอง, CLI flags ของตัวเอง และ plugin ecosystem ของตัวเอง

Rust มาพร้อมเครื่องมือทางการเพียงตัวเดียวคือ Cargo ซึ่งจัดการงานทั้งหมดเหล่านั้น:

งานใน Node / TSเทียบเท่าใน Cargo
npm installcargo add / cargo fetch
tsccargo build
node index.jscargo run
eslintcargo clippy
prettiercargo fmt
jestcargo test
npm publishcargo publish
npx tsdoccargo doc

ไม่มีการ “เลือกเอง” community Rust ใช้ formatter เดียว (rustfmt), linter เดียว (Clippy) และ test runner เดียว ทำให้ทุก Rust project มีหน้าตาคุ้นเคย

  • cargo.mdx — manifest Cargo.toml, Cargo.lock และ subcommand ที่สำคัญ
  • clippy-rustfmt.mdx — lint ด้วย Clippy และ format ด้วย rustfmt เทียบกับ eslint + prettier
  • testing.mdx — unit test ด้วย #[test], integration test และ doctest เทียบกับ Jest
  • workspaces-features.mdx — workspace หลาย crate และ feature flag เทียบกับ npm workspaces
  • release-cross-compile.mdx — release build และ cross-compilation ไปยัง static binary
  • docker-ci.mdx — multi-stage Dockerfile และ GitHub Actions สำหรับ Rust project
TypeScript
# Node / TypeScript
npm init -y
npm install typescript --save-dev
npx tsc --init
# Now you also need eslint, prettier, jest...
Rust
# Rust
cargo new my-app
cd my-app
cargo build # compiles
cargo run # compiles + runs
cargo test # runs all tests
cargo clippy # lints
cargo fmt # formats

นี่คือบทภาพรวมของโมดูล ไม่มี Playground ในบทนี้ — บทถัดไปแต่ละบทจะสาธิต tool เฉพาะ รัน Cargo command ข้างต้นใน terminal ของคุณหลังจากติดตั้ง Rust ผ่าน rustup.rs

Cargo subcommand ใดเทียบเท่ากับการรัน `npx jest`?
เครื่องมือใด format Rust code (เทียบเท่า Prettier)?
ข้อได้เปรียบหลักของ toolchain ทางการเดียวของ Rust เมื่อเทียบกับ Node.js ecosystem คืออะไร?