การติดตั้งและเครื่องมือสำหรับภาษา Rust

Rust Sep 10, 2023

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

Rust Programming Language
A language empowering everyone to build reliable and efficient software.

การติดตั้ง Rust

ติดตั้งผ่าน rustup ได้เลย (หากใช้ Windows ก็จะเป็นไฟล์ .exe)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup.rs - The Rust toolchain installer
The Rust toolchain installer

สิ่งที่เราจะได้มาจากการ install ผ่าน rustup คือ rustccargo, และ rustup

  • rustc - เป็นตัว compiler
  • cargo - เป็น Rust Package Manager
  • rustup - 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

rust-analyzer - Visual Studio Marketplace
Extension for Visual Studio Code - Rust language support for Visual Studio Code

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

แหล่งเรียนรู้เพิ่มเติม

The Rust Programming Language - The Rust Programming Language
Introduction - Rust By Example
Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.
GitHub - rust-lang/rustlings: :crab: Small exercises to get you used to reading and writing Rust code!
:crab: Small exercises to get you used to reading and writing Rust code! - GitHub - rust-lang/rustlings: :crab: Small exercises to get you used to reading and writing Rust code!
Rust Language Cheat Sheet
Rust-101: main.rs
Welcome to Comprehensive Rust 🦀 - Comprehensive Rust 🦀
Tour of Rust - Let’s go on an adventure!
Welcome to the Tour of Rust. This is meant to be a step by step guide through the features of the Rust programming language
Introduction - A Gentle Introduction to Rust
Introduction to the Rust language, standard library and ecosystem
GitHub - ctjhoa/rust-learning: A bunch of links to blog posts, articles, videos, etc for learning Rust
A bunch of links to blog posts, articles, videos, etc for learning Rust - GitHub - ctjhoa/rust-learning: A bunch of links to blog posts, articles, videos, etc for learning Rust
GitHub - rust-unofficial/awesome-rust: A curated list of Rust code and resources.
A curated list of Rust code and resources. Contribute to rust-unofficial/awesome-rust development by creating an account on GitHub.

Tags

Chai Phonbopit

เป็น Web Dev ทำงานมา 10 ปีหน่อยๆ ด้วยภาษา JavaScript, Node.js, React, Vue และปัจจุบันกำลังสนใจ Web3, Crypto และ Blockchain เขียนบล็อกที่ https://devahoy.com