知识卡片
问号传播可恢复错误
对 Result 使用 ? 时,Ok(v) 让当前表达式取到 v,Err(e) 则提前返回给调用者,必要时把 e 转成外层错误类型。支持这种返回形状的函数可串联多步 I/O 而保留各步失败。只会返回 () 的入口需自行处理,或改为返回兼容结果。
关联阅读:[[Result显式表示失败]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- A Tour of Rust 把成功值或 I/O 错误向上传递 · 查看原书依据
If File::create fails, the ? operator returns from write_image, passing along the error. Otherwise, output holds the successfully opened File.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第01章《A Tour of Rust》|把成功值或 I/O 错误向上传递,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch01.xhtml#writing-image-files`
- 结论依据:原文把手写 match 与 File::create 后接问号对照,显示两条路径。
- 原始内容:If File::create fails, the ? operator returns from write_image, passing along the error. Otherwise, output holds the successfully opened File.