← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 1441.h · 生成模型:未记录

移动所有权以提前清理

理解等级:中级

使用 std::mem::drop(value) 提前清理有所有权的值。它消费参数,调用者不再持有该值。value.drop() 会绕开所有权转移、让后续自动清理有重复析构风险,因此语言禁止直接调用析构方法。

相关卡片 [[清理行为绑定对象寿命]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0171) - 结论依据:原文对照禁止手动调用的 Drop 方法与可按值调用的 std::mem::drop。 - 原始内容:The std::mem::drop function is different from the drop method in the Drop trait. We call it by passing as an argument the value we want to force-drop. The function is in the prelude, so we can modify main in Listing 15-15 to call the drop function, as shown in Listing 15-16.