← 返回起始笔记位置

知识卡片

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

问号传播可恢复错误

理解等级:中级

对 Result 使用 ? 时,Ok(v) 让当前表达式取到 v,Err(e) 则提前返回给调用者,必要时把 e 转成外层错误类型。支持这种返回形状的函数可串联多步 I/O 而保留各步失败。只会返回 () 的入口需自行处理,或改为返回兼容结果。

关联阅读:[[Result显式表示失败]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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.