知识卡片
文字错误会丢失类型信息
只需展示错误时,可将底层错误格式化为文字并用实现Error的本地包装类型返回。代价是调用者失去原始子错误的类型和结构,难以可靠分类处理。若此代价可接受,From可配合?转换;否则应保留底层错误,而不是以后解析错误消息。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Minimal Errors · 查看原书依据
Minimal Errors If nested error information isn’t needed, then an implementation of the Error type need not be much more than a String—one rare occasion where a “stringly typed” variable might be appropriate. It does need to be a little more than a String though; while it’s possible to use String as the E type parameter: pub fn find_user(username: &str) -> Re
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Minimal Errors》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u026-ch01-030)。
- 结论依据:原文在“Minimal Errors”附近的定义、例子或代码支撑本卡核心说明:只需展示错误时,可将底层错误格式化为文字并用实现Error的本地包装类型返回。边界按已读前提解释;示例未执行。
- 原始内容:Minimal Errors If nested error information isn’t needed, then an implementation of the Error type need not be much more than a String—one rare occasion where a “stringly typed” variable might be appropriate. It does need to be a little more than a String though; while it’s possible to use String as the E type parameter: pub fn find_user(username: &str) -> Re