Rust
High-performance systems language — ใช้สร้าง trading engine ที่ต้องการ low-latency และ memory safety
What I Can Do
- สร้าง high-performance trading engine ด้วย Rust
- ใช้ ownership system เพื่อ memory safety โดยไม่ต้อง garbage collector
- เขียน concurrent code ด้วย async/await (Tokio runtime)
- Optimize latency-critical paths สำหรับ order matching
- Integrate กับ Go services ผ่าน gRPC/HTTP
Commands I Use Daily
# compile และ run
cargo run
# build release (optimized)
cargo build --release
# run tests
cargo test
# lint — ตรวจ common mistakes
cargo clippy
# format code ตาม standard
cargo fmt
# ตรวจ dependencies ที่มี vulnerabilities
cargo audit
# ดู dependency tree
cargo treeOwnership & Borrowing
Rust ใช้ ownership system แทน garbage collector — ทุก value มี owner เดียว, เมื่อ owner ออกจาก scope value จะถูก drop อัตโนมัติ Borrowing (& / &mut) ให้ยืม reference โดยไม่ย้าย ownership ทำให้ memory safe โดยไม่มี runtime cost
Lifetimes
Lifetimes เป็น annotation ที่บอก compiler ว่า reference จะอยู่ได้นานเท่าไหร่ — ป้องกัน dangling references ที่ compile time ส่วนใหญ่ compiler infer ได้เอง (lifetime elision) แต่บางกรณีต้องระบุเอง เช่น function ที่ return reference
Error Handling (Result / Option)
Result<T, E>— ใช้สำหรับ operations ที่อาจ fail,Ok(value)หรือErr(error)Option<T>— ใช้แทน null,Some(value)หรือNone- ใช้
?operator propagate errors ขึ้นไป caller - ใช้
thiserrorสำหรับ custom error types,anyhowสำหรับ application-level errors
Pattern Matching
match expression ใน Rust ต้อง handle ทุก case (exhaustive) — compiler บังคับให้ไม่ลืม case ใดๆ ใช้ destructure enums, structs, tuples ได้ เช่น OrderStatus::Pending => process(order), OrderStatus::Filled destructure fields ด้วย price, qty, OrderStatus::Cancelled(reason) => log(reason)
Traits
Traits คือ shared behavior (คล้าย interface) — define methods ที่ types ต้อง implement ใช้ trait bounds กำหนดว่า generic type ต้องมี behavior อะไร เช่น fn process ที่กำหนด trait bounds T: Serialize + Send
Structs & Enums
- Structs — custom data types ที่รวม fields เข้าด้วยกัน, ใช้
implblock เพิ่ม methods - Enums — type ที่มีหลาย variants, แต่ละ variant เก็บ data ต่างกันได้ (algebraic data types)
- Enums + pattern matching เป็น core pattern ของ Rust
Async / Await (Tokio)
Rust ใช้ async/await สำหรับ concurrent I/O — ต้องใช้ runtime เช่น Tokio ที่จัดการ scheduling ของ futures, tokio::spawn สร้าง concurrent tasks, tokio::select! รอหลาย futures พร้อมกัน
Concurrency (Send / Sync)
- Send — type สามารถ move ข้าม threads ได้
- Sync — type สามารถ share reference ข้าม threads ได้
- Compiler ตรวจ data races ที่ compile time — ถ้า code compile ได้ = ไม่มี data races
- ใช้
Arc+Mutexสำหรับ shared mutable state ข้าม threads
Unsafe
unsafe block ปลด restrictions บางอย่างของ compiler — ใช้เมื่อต้อง dereference raw pointers, call FFI functions, หรือ implement unsafe traits ควรจำกัดการใช้ให้น้อยที่สุดและ encapsulate ไว้ใน safe abstraction
Performance Optimization
- Zero-cost abstractions — iterators, closures compile เป็น code เดียวกับ manual loops
- ใช้
#[inline]สำหรับ hot functions - Profile ด้วย
cargo flamegraphหา bottlenecks - ใช้ SIMD intrinsics สำหรับ data-parallel operations
Cargo & Ecosystem
- Cargo — build system + package manager, จัดการ dependencies, builds, tests ในที่เดียว
- crates.io — package registry
- Cargo.toml — define dependencies, features, build settings
- Workspaces — จัดการ multi-crate projects
Related Skills
- Go — backend services ที่ communicate กับ trading engine
- Kafka — event streaming ระหว่าง Rust engine กับ Go services
- PostgreSQL — persistent storage สำหรับ trade data