← 返回刚才阅读的位置

知识卡片

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

结果类型承载失败信息

理解等级:初级

Result 让函数返回 Ok(T) 或 Err(E)。T 是完成操作后的数据,E 携带可供调用方判断的失败信息。打开文件成功时得到句柄,失败时可据错误决定补救、上交还是停止。调用者可通过 match 处理两种结果。

相关卡片 [[缺失进入类型系统]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 错误处理 Recoverable Errors with Result · 查看原书依据

      The T and E are generic type parameters: we’ll discuss generics in more detail in Chapter 10. What you need to know right now is that T represents the type of the value that will be returned in a success case within the Ok variant, and E represents the type of the error that will be returned in a failure case within the Err variant.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第09章 错误处理(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c09.xhtml`,块 0038) - 结论依据:原文定义两种类型参数在成功和失败分支中的作用。 - 原始内容:The T and E are generic type parameters: we’ll discuss generics in more detail in Chapter 10. What you need to know right now is that T represents the type of the value that will be returned in a success case within the Ok variant, and E represents the type of the error that will be returned in a failure case within the Err variant.