rust-design-patterns
Installation
SKILL.md
Rust 慣用設計模式
適用場景
- 設計新的 struct/API 時,需要選擇合適的建構方式(Builder / Typestate)
- 需要對原始型別加上語意約束或防止混用(Newtype)
- 管理需要確定釋放的資源(檔案、鎖、連線)→ RAII Guard
- 在不可變結構中仍需局部可變性 → Interior Mutability
- 需要執行時多態策略切換 → Strategy(trait object / enum dispatch)
- 限制 trait 只能在本 crate 實作 → Sealed Trait
- 為外部型別追加方法 → Extension Trait
- 事件驅動回呼架構 → Observer / Command Pattern
- 型別間慣用轉換 → From / Into / TryFrom