การติดตั้งและเครื่องมือสำหรับภาษา Rust
บทความนี้จะพูดถึงขั้นตอนการเตรียมตัว เครื่องมือ การตั้งค่าต่างๆ สำหรับการเขียนโปรแกรมด้วยภาษา Rust ของผมเองนะครับ รวมถึงคำสั่งเบื้องต้นพื้นฐาน แน่นอนแหล่งศึกษาเรียนรู้หลักๆ ก็คือเว็บ Official ของ Rust เองครับ

การติดตั้ง Rust
ติดตั้งผ่าน rustup ได้เลย (หากใช้ Windows ก็จะเป็นไฟล์ .exe)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
สิ่งที่เราจะได้มาจากการ install ผ่าน rustup คือ rustc
, cargo
, และ rustup
rustc
- เป็นตัว compilercargo
- เป็น Rust Package Managerrustup
- Rust toolchain installer - เป็นตัวติดตั้ง rust และจัดการ toolchain ต่างๆ หรือการอัพเดทเวอร์ชั่นของ rust เป็นต้น
Verify ว่าติดตั้ง Rust ถูกต้อง ด้วยคำสั่ง rustc
หรือ cargo
rustc --version
# ผลลัพธ์
rustc 1.72.0 (5680fa18f 2023-08-23)
cargo --version
cargo 1.72.0 (103a7ff2e 2023-08-15)
การอัพเดท Rustup
การอัพเดท Rust และ toolchains ต่างๆ
rustup update
การอัพเดทเวอร์ชั่นของ rustup
rustup self update
การสร้างโปรเจ็คใหม่ ด้วย Cargo
cargo new <project_name>
เมื่อสร้างโปรเจ็คด้วย cargo จะมี 2 ไฟล์หลักๆ คือ Cargo.toml
และ src/main.rs
├── Cargo.toml
└── src
└── main.rs
ตัวอย่างโปรแกรม สำหรับแสดง Hello, world! ที่หน้าจอ
fn main() {
println!("Hello, world!");
}
ทดลองรันโปรแกรมด้วยคำสั่ง
cargo run
ตัวคำสั่งจะทำการ compile rust และ execute ไฟล์ ให้เรา ถ้าเราไม่ใช้ cargo เราก็ต้อง compile และรันเอง แบบนี้
# compile
rustc src/main.rs
# หรือระบุชื่อ output filename
rustc src/main.rs -o hello
# excecute
./main
# ถ้าบน windows จะเป็น main.exe
./main.exe
Cargo.toml
ตัว Cargo.toml
เรียกว่า manifest เป็นเหมือน metadata ของโปรเจ็คเรา ที่ระบุว่าชื่อโปรเจ็คอะไร เวอร์ชั่นไหน ใช้ dependencies อะไรบ้าง ตัวไฟล์จะเป็นรูปแบบ TOML ถ้าฝั่ง Node.js ก็เปรียบเสมือน package.json
ครับ ตัวอย่าง
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
IDE & Text Editor
ใช้ VS Code + Rust Analyzer
Settings ใช้ default format เป็น rust-analyzer และตั้งแบบ format on save + auto save (ไฟล์ settings.json ของ VS Code)
{
"files.autoSave": "onFocusChange",
"editor.formatOnSave": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
}
}
Cargo Watch
ใช้สำหรับ development เวลาที่เรากำลังพัฒนาอยู่ ไม่ต้องเสียเวลา compile และ run คล้ายๆกับ Nodemon ของฝั่ง Node.js คือถ้ามีไฟล์ถูกแก้ไข มันก็จะรันอัตโนมัติ ให้เราเลย
ติดตั้ง cargo-watch
cargo install cargo-watch
รันคำสั่ง ให้ cargo run
ทุกครั้งที่ไฟล์ changed
cargo watch -x run
หรือ หลายๆคำสั่งต่อกัน เช่น รัน test ก่อน ค่อย run
cargo watch -x test -x run
Test
เทสจะใช้คำสั่ง (โปรเจ็คที่สร้างด้วย cargo new
)
cargo test
Code Coverage จะใช้ตัว tarpaulin ทำการติดตั้งผ่าน cargo:
cargo install cargo-tarpaulin
รัน coverage
# output เป็น xml
cargo tarpaulin --out Xml
# output เป็น html
cargo tarpaulin --out html
Linting และ Formatting
ปกติถ้าเขียน Node.js / JavaScript พวก Linting และ Formatting คงจะหนีไม่พ้น ESLint และ Prettier สำหรับใน Rust ตัว Linter เราจะใช้ clippy ครับ และ formatting จะใช้ตัวที่ชื่อว่า rustfmt
ตัว clippy และ rustfmt ทั้งคู่ เป็น official rust linter/formatting ดูแลโดยทีม Rust โดยตรง ติดตั้งด้วยคำสั่งผ่าน rustup:
rustup component add clippy
วิธีใช้งาน คือรัน
cargo clippy
การติดตั้ง rustfmt ผ่าน rustup
rustup component add rustfmt
ทำการ format (ปกติถ้าใช้ VS Code + Rust Analyser น่าจะถุก auto format) ด้วยคำสั่ง
cargo fmt
# ใช้แบบ CI
cargo fmt -- --check
แหล่งเรียนรู้เพิ่มเติม

