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

Setup — การติดตั้ง Rust และ Toolchain

หมายเหตุ Playground: บทเรียนนี้เกี่ยวกับการตั้งค่าและ Toolchain — คำสั่งต่างๆ รันใน Terminal ของคุณ ไม่ใช่ในเบราว์เซอร์ จึงไม่มี Playground snippet ที่รันได้ในที่นี้

ในฐานะ 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 / NodeRustวัตถุประสงค์
nvm / voltarustupจัดการเวอร์ชัน Language/Toolchain
nodecargo runรันโปรแกรม
tsccargo buildCompile Source เป็น Output
npm installcargo add <crate>เพิ่ม Dependency
package.jsonCargo.tomlProject manifest และรายการ Dependencies
node_modules/~/.cargo/registry/ + target/Cached crates และ Build artifacts
npm run <script>cargo run / custom tasksรัน Project scripts
npx tsc --initcargo new <name>Scaffold โปรเจกต์ใหม่
eslint / prettiercargo clippy / cargo fmtLinter และ Formatter
jest / vitestcargo testรัน Tests

วิธีการอย่างเป็นทางการในการติดตั้ง Rust คือผ่าน rustup ที่เป็น Rust toolchain installer รันคำสั่งนี้ใน Terminal:

Terminal window
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

ทำตาม Prompts (การติดตั้งแบบ Default ก็เหมาะสมแล้ว) จากนั้น Restart shell หรือรัน:

Terminal window
source $HOME/.cargo/env

ยืนยันการติดตั้ง:

Terminal window
rustc --version # rustc 1.xx.x (...)
cargo --version # cargo 1.xx.x (...)

rustup ติดตั้ง stable toolchain โดยค่าเริ่มต้น คุณสามารถเพิ่ม nightly ในภายหลังหากต้องการ Feature ทดลอง — แต่ stable คือสิ่งที่ต้องการสำหรับคอร์สนี้

คำสั่ง cargo new สร้าง Project เหมือน npm init แต่มี Convention ที่มากกว่า:

TypeScript
# TypeScript workflow
mkdir my-project && cd my-project
npm init -y # creates package.json
npm install typescript --save-dev
npx tsc --init # creates tsconfig.json
# ... write src/index.ts ...
npx tsc # compile
node dist/index.js # run
Rust
# Rust workflow
cargo new my-project # creates the project
cd my-project
# ... edit src/main.rs ...
cargo run # compile + run in one step
cargo build # compile only (output in target/)
cargo build --release # optimised production build

การเพิ่ม Dependency ใน Rust ทำได้ด้วยคำสั่งเดียว:

Terminal window
cargo add serde # adds serde to Cargo.toml
cargo add serde --features derive # with a feature flag
cargo add tokio --features full # tokio async runtime

เทียบเท่ากับ npm install serde โดย Cargo จะอัปเดตไฟล์ Cargo.toml ให้อัตโนมัติ เหมือนกับ package.json

Terminal window
cargo new <name> # scaffold a binary project
cargo new --lib <name> # scaffold a library
cargo build # compile (debug mode, fast)
cargo build --release # compile (release mode, optimised)
cargo run # compile + run
cargo test # run tests
cargo add <crate> # add a dependency
cargo fmt # format all source files
cargo clippy # lint (like eslint for Rust)
cargo doc --open # build and open local docs
cargo check # type-check without producing a binary (fast)

cargo check มีประโยชน์เป็นพิเศษ: รัน Borrow checker และ Type checker ให้ครบ โดยไม่ Compile ออกมาเป็น Machine code จริง จึงเร็วกว่า cargo build มาก เป็นคำสั่งที่ใช้บ่อยที่สุดระหว่าง Iterate — เทียบได้กับการรัน tsc --noEmit

เพื่ออัปเดต Toolchain ที่ติดตั้งไว้:

Terminal window
rustup update # update stable (and any other installed toolchains)
rustup show # list installed toolchains
rustup default stable # set the default toolchain

นี่คือ Equivalent ของ nvm install --lts && nvm use --lts

อะไรคือ Rust เทียบเท่ากับ nvm หรือ volta สำหรับการจัดการเวอร์ชัน Toolchain?
คำสั่งใด Compile และรัน Rust project ของคุณทันที?
วิธีที่เร็วที่สุดในการ Type-check Rust project โดยไม่สร้าง Binary คืออะไร?
คุณเพิ่ม Dependency ชื่อ `serde` ให้กับ Rust project ได้อย่างไร?