知识卡片

普通读书笔记卡 · 1441.b.1

可失败释放需要显式接口

理解等级:高级

Drop::drop没有返回Result的通道,无法把释放失败直接交给调用方处理。若关闭文件、提交或释放动作的失败必须被看见,应提供显式方法返回结果,同时设计析构时的兜底清理。不要把可观察的业务成功寄托在一个无法报告失败的析构方法上。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Item 11: Implement the Drop trait for RAII patterns》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u067-ch02-016)。 - 结论依据:原文在“no way to signal failure”附近的定义、例子或代码支撑本卡核心说明:Drop::drop没有返回Result的通道,无法把释放失败直接交给调用方处理。边界按已读前提解释;示例未执行。 - 原始内容:no way to signal failure. If an attempt to release resources can fail, then you should probably have a separate release method that returns a Result, so it’s possible for users to detect this failure. Regardless of the technical details, the drop method is nevertheless the key place for implementing RAII patterns; its implementation is the ideal place to rel