← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.12.a.1.g.1.b · 生成模型:GPT-6

Result与panic的边界

理解等级:高级

预期会发生的输入错误或业务失败适合用 Result<T, E>。签名里出现 Result,调用者就知道要处理 Ok(T) 与 Err(E)。无法继续工作的基础配置缺失,或应由程序不变量保证的路径,可以用 panic 信号中止当前执行路径。示例在确认字符是指定进制的数字后才取数字值,若用 expect,写清此前检查为何保证成功,可以帮助诊断不变量破坏。

相关卡片 [[以返回值表示失败]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • From panic to result: Error handling Result和可接受的panic · 查看原书依据

      The reader also knows that there is an alternative to Result: just panic! In theory—similar to Go—panic is reserved for situations where you think it is best to stop the program altogether because something that really should not happen happened. A normal failure case is a customer not having enough money in their account or an order being out of stock. Panicking is what you would do if a customer just purchased an order worth infinite dollars.

    参考来源

    - 位置:《Write Powerful Rust Macros》第07章《From panic to result: Error handling》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-7.xhtml#p47) - 结论依据:7.4 把日常验证失败与无法继续或违反不变量的情况分开,附 expect 用来表达不变量依据的建议。 - 原始内容:The reader also knows that there is an alternative to Result: just panic! In theory—similar to Go—panic is reserved for situations where you think it is best to stop the program altogether because something that really should not happen happened. A normal failure case is a customer not having enough money in their account or an order being out of stock. Panicking is what you would do if a customer just purchased an order worth infinite dollars.