知识卡片

普通读书笔记卡 · 743.d.2.a.2 · 生成模型:未记录

问号提前返回转换错误

理解等级:中级

Result 后写 ?,Ok 时表达式得到其内部值,Err 时整个函数提前返回。若当前函数声明另一种错误类型,From 转换可以把底层错误包装成统一的外层错误。可以接着链式调用下一步,它只会在前一步成功时运行。

相关卡片 [[错误传播保留调用者决定]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 错误处理 Propagating Errors · 查看原书依据

      There is a difference between what the match expression from Listing 9-6 does and what the ? operator does: error values that have the ? operator called on them go through the from function, defined in the From trait in the standard library, which is used to convert values from one type into another. When the ?

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第09章 错误处理(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c09.xhtml`,块 0097) - 结论依据:原文说明问号与手写 match 的差别在于错误通过 From 转换。 - 原始内容:There is a difference between what the match expression from Listing 9-6 does and what the ? operator does: error values that have the ? operator called on them go through the from function, defined in the From trait in the standard library, which is used to convert values from one type into another. When the ?