知识卡片
结果类型承载失败信息
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.