知识卡片

普通读书笔记卡 · 743.d.2.b

标准错误提供统一接口

理解等级:中级

Result并不要求E实现Error,但自定义错误实现标准Error可被通用处理器识别。它要求Debug与Display,并可通过source暴露底层错误,默认无来源。书中把Error限于std的说明已过时:core::error::Error自Rust1.81稳定,no_std可用性应按目标工具链核对。

版本依据:core::error::Error官方文档,2026-10-27查阅。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types The Error Trait · 查看原书依据

      The Error Trait It’s always good to understand what the standard traits (Item 10) involve, and the relevant trait here is std::error::Error. The E type parameter for a Result doesn’t have to be a type that implements Error, but it’s a common convention that allows wrappers to express appropriate trait bounds—so prefer to implement Error for your error types.

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《The Error Trait》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u025-ch01-028)。 - 结论依据:原文在“The Error Trait”附近的定义、例子或代码支撑本卡核心说明:Result<T,E>并不要求E实现Error,但自定义错误实现标准Error可被通用处理器识别。边界按已读前提解释;示例未执行。 - 原始内容:The Error Trait It’s always good to understand what the standard traits (Item 10) involve, and the relevant trait here is std::error::Error. The E type parameter for a Result doesn’t have to be a type that implements Error, but it’s a common convention that allows wrappers to express appropriate trait bounds—so prefer to implement Error for your error types.