知识卡片

普通读书笔记卡 · 743.d.4.a

可恢复失败应保留结果契约

理解等级:中级

Result把正常可预期失败显式交给调用者,便于传播、恢复或展示。panic适合无法继续满足内部假设等情况,选择它就应接受当前线程或进程的终止策略。即使控制main,也不能仅凭位置判断panic一定合适;仍需考虑服务可用性和状态。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 3. Concepts Item 18: Don’t panic · 查看原书依据

      prefer returning a Result to using panic! (but don’t panic is much catchier). Rust’s panic mechanism is primarily designed for unrecoverable bugs in your program, and by default it terminates the thread that issues the panic!. However, there are alternatives to this default. In particular, newcomers to Rust who have come from languages that have an exception

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Item 18: Don’t panic》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u106-ch03-042)。 - 结论依据:原文在“prefer returning a Result”附近的定义、例子或代码支撑本卡核心说明:Result把正常可预期失败显式交给调用者,便于传播、恢复或展示。边界按已读前提解释;示例未执行。 - 原始内容:prefer returning a Result to using panic! (but don’t panic is much catchier). Rust’s panic mechanism is primarily designed for unrecoverable bugs in your program, and by default it terminates the thread that issues the panic!. However, there are alternatives to this default. In particular, newcomers to Rust who have come from languages that have an exception