📌 ทำไมต้องใช้ Rust สำหรับ Automation?
หลายปีที่ผ่านมา งาน Automation มักใช้ Bash script หรือ Python script เป็นหลัก เพราะเขียนง่ายและมีเครื่องมือเยอะ แต่ปัจจุบัน Rust กลายเป็นทางเลือกที่น่าสนใจ เพราะมันรวดเร็ว เสถียร และปลอดภัยจาก bug ที่เกิดจาก memory หรือ type error ได้ดีกว่า โดยเฉพาะในระบบ production หรือ CI/CD ที่ต้องการความแม่นยำ
⚙ ตัวอย่าง: ใช้ Rust แทน Bash
สมมุติว่าเราต้องการ list ไฟล์ใน directory และกรองเฉพาะไฟล์ที่ลงท้ายด้วย “.log”
Bash:
ls *.log | grep "error"
Rust:
use std::fs; use std::path::Path; fn main() { let paths = fs::read_dir(".").unwrap(); for path in paths { let p = path.unwrap().path(); if let Some(ext) = p.extension() { if ext == "log" { println!("{}", p.display()); } } } }
🧪 ตัวอย่าง: ส่ง HTTP request แบบ Python
Python (ใช้ requests):
import requests r = requests.get('https://httpbin.org/get') print(r.text)
Rust (ใช้ reqwest):
// ใน Cargo.toml // reqwest = { version = "0.11", features = ["blocking"] } use reqwest; fn main() { let body = reqwest::blocking::get("https://httpbin.org/get") .unwrap() .text() .unwrap(); println!("{}", body); }
📂 จัดโครงสร้าง Automation Scripts แบบมืออาชีพ
- แยก logic ออกเป็น module ย่อย
- ใช้ config จากไฟล์ JSON หรือ YAML
- เพิ่ม logging และ error handling ให้พร้อมใช้งานใน production
🧰 ไลบรารีแนะนำสำหรับ Automation ด้วย Rust
clap
— จัดการ arguments CLIserde_json / serde_yaml
— อ่านไฟล์ configreqwest
— ส่ง HTTP requeststokio
— async runtime สำหรับงานแบบ concurrentrayon
— parallel processing
🔁 Automation ที่ Rust ทำได้ดีกว่า
- Build & Deploy Automation ในระบบ CI
- การจัดการไฟล์จำนวนมาก (rename, move, filter)
- เชื่อมต่อ API เพื่อ sync ข้อมูลอัตโนมัติ
- แปลงข้อมูลหรือไฟล์ log อย่างรวดเร็ว
- พัฒนาเครื่องมือภายใน (internal tooling)
💬 สรุป
Rust อาจจะดูเขียนยากในตอนแรกเมื่อเทียบกับ Bash หรือ Python แต่ข้อดีของมันในด้าน performance และ safety ทำให้มันเหมาะอย่างยิ่งสำหรับงาน Automation ที่ต้องการ reliability สูง ถ้าคุณกำลังมองหาวิธีทำ Automation ที่ทั้งเร็วและมั่นใจได้ในผลลัพธ์ — Rust คือคำตอบที่ควรลอง
บทความนี้ใช้เวลาอ่านประมาณ 20 นาที โดยทีมงาน poolsawat.com