Goal
พัฒนา Rust ตาม best practices สำหรับ systems programming ที่เน้น memory safety, performance และ concurrency โดยไม่ต้องมี garbage collector โดยใช้ Rust 1.85.0+ และ Edition 2024
Scope
ใช้สำหรับการพัฒนา Rust ทุกประเภท เช่น systems programming, WebAssembly, CLI tools, network services, async applications, และ embedded systems
Execute
1. Install Rust Toolchain
ติดตั้ง Rust toolchain ด้วย rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup update stable
ตรวจสอบ version ด้วย rustc --version (ควรเป็น 1.85.0 ขึ้นไป)
2. Create Project
สร้าง project ใหม่ด้วย cargo
cargo new project-name --edition 2024
cd project-name
ตั้งค่า edition = "2024" ใน Cargo.toml สำหรับใช้ features ล่าสุด
3. Learn Fundamentals
เรียนรู้พื้นฐาน Rust จาก guide/ directory
- อ่าน
guide/getting-started/สำหรับภาพรวมและการติดตั้ง - อ่าน
guide/core-language/สำหรับ language features - อ่าน
key-concepts/สำหรับ ownership, borrowing, lifetimes - อ่าน
principles/สำหรับ design principles
4. Write Code
เขียน code ตาม best practices
- ทำตาม
guide/best-practices/สำหรับ code organization และ performance - ทำตาม
guide/error-handling/สำหรับ error handling - ใช้
Result<T, E>สำหรับ recoverable errors - ใช้
Option<T>สำหรับ optional values - ใช้
?operator สำหรับ error propagation
5. Async Programming
เขียน async code ตาม production patterns
- ใช้ bounded channels (
mpsc::channel(N)) สำหรับ backpressure - ใช้
JoinSetสำหรับ structured concurrency - ใช้
tokio::spawnภายในJoinSetเพื่อหลีกเลี่ยง orphan tasks - ทำให้
select!arms เป็น cancellation-safe - ใช้
spawn_blockingสำหรับ blocking operations - ใช้
tokio::sync::Mutexถ้า lock ข้าม.await - เพิ่ม
#[tracing::instrument]สำหรับ debugging
6. Test And Build
ทดสอบและ build project
cargo test
cargo test --release
cargo clippy
cargo check
cargo fmt
ใช้ scripts/check.sh และ scripts/test.sh สำหรับ automation
7. Use Workflows
ใช้ workflows สำหรับ development lifecycle
- อ่าน
workflows/setup-rust-project.mdสำหรับ setup project - อ่าน
workflows/build-rust-project.mdสำหรับ build project - อ่าน
workflows/test-rust-project.mdสำหรับ test project
Rules
Naming And Style
- ใช้
letสำหรับ immutable variables,let mutเฉพาะเมื่อจำเป็น - ใช้ snake_case สำหรับ variables/functions, PascalCase สำหรับ types, SCREAMING_SNAKE_CASE สำหรับ constants
- ใช้
#[expect]แทน#[allow]เมื่อต้องการ lint warnings - ใช้ rustfmt ด้วย style edition 2024 สำหรับ formatting ที่ทันสมัย
Ownership And Memory
- ใช้ ownership rules อย่างเคร่งครัด, ใช้ borrowing สำหรับ temporary access
- ใช้ lifetimes annotations เมื่อจำเป็น, หลีกเลี่ยง cloning เมื่อเป็นไปได้
- ใช้ references แทน ownership transfer
- ใช้
impl Trait + use<>สำหรับ precise lifetime capturing (Rust 2024) - หลีกเลี่ยง
refและ&ภายใน implicitly-borrowing patterns (Rust 2024) - ใช้
matchแทนif letเมื่อต้องการ preserve drop order (Rust 2024) - ใช้
&raw constและ&raw mutสำหรับ explicit raw pointers (Rust 1.82+)
Error Handling
- ใช้
Result<T, E>สำหรับ recoverable errors,Option<T>สำหรับ optional values - ใช้
?operator สำหรับ error propagation, ใช้unwrap()เฉพาะใน tests - ใช้ custom error types สำหรับ specific errors
- ใช้
core::errormodule สำหรับ error handling ที่ดีขึ้น (Rust 1.81+) - ใช้
Result::inspectและOption::inspectสำหรับ debugging (Rust 1.76+) - ใช้
Option::is_none_orสำหรับ concise optional checks (Rust 1.82+)
Concurrency
- ใช้ threads สำหรับ parallel execution, channels สำหรับ message passing
- ใช้ mutex สำหรับ shared state, atomic types สำหรับ lock-free programming
- หลีกเลี่ยง data races
- ใช้ bounded channels (
mpsc::channel(N)) สำหรับ backpressure - ใช้
JoinSetสำหรับ structured concurrency - ใช้
tokio::spawnภายในJoinSetเพื่อหลีกเลี่ยง orphan tasks - ใช้
spawn_blockingสำหรับ blocking operations - ใช้
tokio::sync::Mutexถ้า lock ข้าม.await
Async Patterns
- ทำให้
select!arms เป็น cancellation-safe - เพิ่ม
#[tracing::instrument]สำหรับ debugging async code - ใช้
async fnและimpl Traitใน traits สำหรับ async programming - ใช้ async closures
async || {}สำหรับ borrowing from closure captures (Rust 1.85+) - ใช้
AsyncFn,AsyncFnMut,AsyncFnOncetraits สำหรับ async closures (Rust 1.85+) - ใช้
FutureและIntoFutureจาก prelude (Rust 2024) - ใช้
std::task::Waker::noopสำหรับ no-op wakers (Rust 1.85+)
Standard Library Features
- ใช้
let_chainsสำหรับ conditional logic ที่กระชับ (Rust 1.88+) - ใช้
HashMap::extract_ifสำหรับ conditional removal (Rust 1.88+) - ใช้
Arc::unwrap_or_cloneสำหรับ efficient cloning (Rust 1.76+) - ใช้
Cell::update,HashSet::extract_if(Rust 1.88+) - ใช้
core::errormodule สำหรับ error handling ที่ดีขึ้น (Rust 1.81+) - ใช้
LazyCellสำหรับ lazy initialization (Rust 1.80+) - ใช้
impl IntoIterator for Box<[T]>(Rust 1.80+) - ใช้
&raw constและ&raw mutoperators (Rust 1.82+) - ใช้ floating-point arithmetic ใน
const fn(Rust 1.82+) - ใช้
#[target_feature]บน safe functions (Rust 1.86+)
Unsafe Code
- ใช้
unsafe externblocks สำหรับ FFI (Rust 2024) - ใช้
unsafekeyword สำหรับexport_sectionและno_mangleattributes (Rust 2024) - ใช้
std::env::set_var,std::env::remove_varด้วยunsafe(Rust 2024) - ตรวจสอบ
unsafe_op_in_unsafe_fnlint warnings (Rust 2024)
Cargo Configuration
- ตั้งค่า
rust-versionใน Cargo.toml สำหรับ dependency resolution ที่ดีขึ้น (Rust 2024) - ใช้
config includekey สำหรับ sharing configurations (Rust 1.80+) - ตรวจสอบ
default-features = falseกับ workspace dependencies (Rust 2024)
Expected Outcome
- Rust code ที่เป็นมาตรฐานและ maintainable
- Memory safety ที่รับประกันโดย compiler
- High performance ด้วย zero-cost abstractions
- Fearless concurrency
- Error handling ที่เหมาะสม
- Code ที่ผ่านการทดสอบและ debug แล้ว
- Understanding ที่ลึกซึ้งเกี่ยวกับ ownership, borrowing, และ lifetimes