rust-error-handling

Installation
SKILL.md

Rust 錯誤處理 (rust-error-handling)

適用場景

  • 設計函式的錯誤回傳類型
  • 選擇 thiserror(library)vs anyhow(application)
  • 實作自訂 Error enum
  • 使用 ? 運算子串接錯誤傳播
  • 將 panic/unwrap 替換為正確的錯誤處理

核心知識

錯誤處理策略選擇

場景 推薦方案
Library crate thiserror 定義具體 Error enum
Application crate anyhow::Result 快速開發
原型開發 .unwrap() / .expect("msg")
不可能失敗 .unwrap() 加註解說明為何安全
需要 backtrace anyhow 或 color-eyre
Installs
1
First Seen
Oct 3, 2026