知识卡片
错误枚举保留失败类别
错误枚举可分别存放I/O错误、UTF-8错误和业务说明,让调用者按类别处理,而不是解析字符串。Display负责展示,source可返回包装的底层错误。保留错误对象有助诊断,但哪些细节属于公共接口、哪些应只用于内部追踪,还要按库的边界选择。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Nested Errors · 查看原书依据
pub enum MyError { Io(std::io::Error), Utf8(std::string::FromUtf8Error), General(String), } This enum definition includes a derive(Debug), but to satisfy the Error trait, a Display implementation is also needed: impl std::fmt::Display for MyError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { MyError::Io(e) => write!(f, "
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Nested Errors》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u027-ch01-031)。
- 结论依据:原文在“pub enum MyError”附近的定义、例子或代码支撑本卡核心说明:错误枚举可分别存放I/O错误、UTF-8错误和业务说明,让调用者按类别处理,而不是解析字符串。边界按已读前提解释;示例未执行。
- 原始内容:pub enum MyError { Io(std::io::Error), Utf8(std::string::FromUtf8Error), General(String), } This enum definition includes a derive(Debug), but to satisfy the Error trait, a Display implementation is also needed: impl std::fmt::Display for MyError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { MyError::Io(e) => write!(f, "