← 返回刚才阅读的位置

知识卡片

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

按结果决定进程退出

理解等级:中级

unwrap_or_else(|err| …) 在 Ok 时抽出成功值,仅在 Err 时执行闭包。CLI 可在闭包中呈现解析错误并调用 process::exit(1),非零退出状态给脚本或父进程处理,消息供人阅读。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • I/O 项目:构建命令行程序 Fixing the Error Handling · 查看原书依据

      In this listing, we’ve used a method we haven’t covered in detail yet: unwrap_or_else, which is defined on Result<T, E> by the standard library ❷. Using unwrap_or_else allows us to define some custom, non-panic! error handling. If the Result is an Ok value, this method’s behavior is similar to unwrap: it returns the inner value that Ok is wrapping.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第12章 I/O 项目:构建命令行程序(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c12.xhtml`,块 0128) - 结论依据:原文逐分支说明 `unwrap_or_else` 如何处理配置或传递错误给闭包。 - 原始内容:In this listing, we’ve used a method we haven’t covered in detail yet: unwrap_or_else, which is defined on Result<T, E> by the standard library ❷. Using unwrap_or_else allows us to define some custom, non-panic! error handling. If the Result is an Ok value, this method’s behavior is similar to unwrap: it returns the inner value that Ok is wrapping.