Setup — การติดตั้ง Rust และ Toolchain
หมายเหตุ Playground: บทเรียนนี้เกี่ยวกับการตั้งค่าและ Toolchain — คำสั่งต่างๆ รันใน Terminal ของคุณ ไม่ใช่ในเบราว์เซอร์ จึงไม่มี Playground snippet ที่รันได้ในที่นี้
จาก npm สู่ cargo — การเปรียบเทียบ Toolchain
หัวข้อที่มีชื่อว่า “จาก npm สู่ cargo — การเปรียบเทียบ Toolchain”ในฐานะ TypeScript developer คุณมี Mental model สำหรับ Language toolchain อยู่แล้ว: nvm หรือ volta จัดการ Node versions, npm/pnpm/yarn จัดการ Packages, tsc Compile TypeScript, และ node รัน Output Rust มีคำตอบที่สะอาดและเป็น Single-tool สำหรับทั้งหมดนี้: rustup และ cargo
| TypeScript / Node | Rust | วัตถุประสงค์ |
|---|---|---|
nvm / volta | rustup | จัดการเวอร์ชัน Language/Toolchain |
node | cargo run | รันโปรแกรม |
tsc | cargo build | Compile Source เป็น Output |
npm install | cargo add <crate> | เพิ่ม Dependency |
package.json | Cargo.toml | Project manifest และรายการ Dependencies |
node_modules/ | ~/.cargo/registry/ + target/ | Cached crates และ Build artifacts |
npm run <script> | cargo run / custom tasks | รัน Project scripts |
npx tsc --init | cargo new <name> | Scaffold โปรเจกต์ใหม่ |
eslint / prettier | cargo clippy / cargo fmt | Linter และ Formatter |
jest / vitest | cargo test | รัน Tests |
การติดตั้ง Rust
หัวข้อที่มีชื่อว่า “การติดตั้ง Rust”วิธีการอย่างเป็นทางการในการติดตั้ง Rust คือผ่าน rustup ที่เป็น Rust toolchain installer รันคำสั่งนี้ใน Terminal:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shทำตาม Prompts (การติดตั้งแบบ Default ก็เหมาะสมแล้ว) จากนั้น Restart shell หรือรัน:
source $HOME/.cargo/envยืนยันการติดตั้ง:
rustc --version # rustc 1.xx.x (...)cargo --version # cargo 1.xx.x (...)rustup ติดตั้ง stable toolchain โดยค่าเริ่มต้น คุณสามารถเพิ่ม nightly ในภายหลังหากต้องการ Feature ทดลอง — แต่ stable คือสิ่งที่ต้องการสำหรับคอร์สนี้
โปรเจกต์แรก — cargo new
หัวข้อที่มีชื่อว่า “โปรเจกต์แรก — cargo new”คำสั่ง cargo new สร้าง Project เหมือน npm init แต่มี Convention ที่มากกว่า:
# TypeScript workflowmkdir my-project && cd my-projectnpm init -y # creates package.jsonnpm install typescript --save-devnpx tsc --init # creates tsconfig.json# ... write src/index.ts ...npx tsc # compilenode dist/index.js # run# Rust workflowcargo new my-project # creates the projectcd my-project# ... edit src/main.rs ...cargo run # compile + run in one stepcargo build # compile only (output in target/)cargo build --release # optimised production buildcargo add — การเพิ่ม Dependencies
หัวข้อที่มีชื่อว่า “cargo add — การเพิ่ม Dependencies”การเพิ่ม Dependency ใน Rust ทำได้ด้วยคำสั่งเดียว:
cargo add serde # adds serde to Cargo.tomlcargo add serde --features derive # with a feature flagcargo add tokio --features full # tokio async runtimeเทียบเท่ากับ npm install serde โดย Cargo จะอัปเดตไฟล์ Cargo.toml ให้อัตโนมัติ เหมือนกับ package.json
คำสั่ง cargo หลัก
หัวข้อที่มีชื่อว่า “คำสั่ง cargo หลัก”cargo new <name> # scaffold a binary projectcargo new --lib <name> # scaffold a librarycargo build # compile (debug mode, fast)cargo build --release # compile (release mode, optimised)cargo run # compile + runcargo test # run testscargo add <crate> # add a dependencycargo fmt # format all source filescargo clippy # lint (like eslint for Rust)cargo doc --open # build and open local docscargo check # type-check without producing a binary (fast)cargo check มีประโยชน์เป็นพิเศษ: รัน Borrow checker และ Type checker ให้ครบ โดยไม่ Compile ออกมาเป็น Machine code จริง จึงเร็วกว่า cargo build มาก เป็นคำสั่งที่ใช้บ่อยที่สุดระหว่าง Iterate — เทียบได้กับการรัน tsc --noEmit
การอัปเดต Rust
หัวข้อที่มีชื่อว่า “การอัปเดต Rust”เพื่ออัปเดต Toolchain ที่ติดตั้งไว้:
rustup update # update stable (and any other installed toolchains)rustup show # list installed toolchainsrustup default stable # set the default toolchainนี่คือ Equivalent ของ nvm install --lts && nvm use --lts