知识卡片
提前释放应消费拥有值
不能直接调用对象的Drop::drop方法,因为仅借用清理后对象仍可能被继续使用并再次析构。std::mem::drop接收拥有值,值移入该函数后正常析构,从类型层面终止后续使用。若只传引用,释放的只是引用值,不会销毁仍被拥有的对象。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 2. Traits Item 11: Implement the Drop trait for RAII patterns · 查看原书依据
drop(_item: T) is just an empty body { }—so the moved item is dropped when that scope’s closing brace is reached. Notice also that the signature of the drop(&mut self) method has no return type, which means that it has 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
参考来源
- 位置:《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-015)。
- 结论依据:原文在“drop(_item: T)”附近的定义、例子或代码支撑本卡核心说明:不能直接调用对象的Drop::drop方法,因为仅借用清理后对象仍可能被继续使用并再次析构。边界按已读前提解释;示例未执行。
- 原始内容:drop(_item: T) is just an empty body { }—so the moved item is dropped when that scope’s closing brace is reached. Notice also that the signature of the drop(&mut self) method has no return type, which means that it has 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