rust-error-advanced
Result vs Option vs panic
| 类型 | 何时使用 | 示例 |
|---|---|---|
Result<T, E> |
预期会失败的操作 | 文件读取、网络请求 |
Option<T> |
absence 正常 | 查找、可能为空的值 |
panic! |
bug 或不变式违规 | 程序逻辑错误、不可恢复错误 |
unreachable!() |
理论上不会执行到的代码 | 匹配穷举 |
错误处理决策
失败是预期的吗?
│
├─ 是 → 这是库代码?
│ ├─ 是 → thiserror(类型化错误)
│ └─ 否 → anyhow(易用性)
More from huiali/rust-skills
rust-performance
Performance optimization expert covering profiling, benchmarking, memory allocation, SIMD, cache optimization, false sharing, lock contention, and NUMA-aware programming.
17rust-anti-pattern
Rust anti-patterns and common mistakes expert. Handles code review issues with clone abuse, unwrap in production, String misuse, index loops, and refactoring guidance.
14rust-actor
Actor model expert covering message passing, state isolation, supervision trees, deadlock prevention, fault tolerance, Actix framework, and Erlang-style concurrency patterns.
13rust-ffi
FFI cross-language interop expert covering C/C++ bindings, bindgen, cbindgen, PyO3, JNI, memory layout, data conversion, and safe FFI patterns.
13rust-ecosystem
Rust ecosystem expert covering crate selection, library recommendations, framework comparisons, async runtime choices (tokio, async-std), and common tools.
13rust-type-driven
Type-driven design expert covering newtype pattern, type state, PhantomData, marker traits, builder pattern, compile-time validation, sealed traits, and zero-sized types (ZST).
13