← 返回刚才阅读的位置

知识卡片

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

清理行为绑定对象寿命

理解等级:中级

为类型实现 Drop::drop(&mut self) 可让资源清理随对象销毁运行。作用域中的 c、d 按声明逆序析构,类型自己的清理逻辑可用于文件、连接或锁。编译器通过所有权管理值的最终清理。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 智能指针 Running Code on Cleanup with the Drop Trait · 查看原书依据

      Rust automatically called drop for us when our instances went out of scope, calling the code we specified. Variables are dropped in the reverse order of their creation, so d was dropped before c. This example’s purpose is to give you a visual guide to how the drop method works; usually you would specify the cleanup code that your type needs to run rather than a print message.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0160) - 结论依据:原文用打印输出确认作用域末尾自动析构及局部变量的逆序。 - 原始内容:Rust automatically called drop for us when our instances went out of scope, calling the code we specified. Variables are dropped in the reverse order of their creation, so d was dropped before c. This example’s purpose is to give you a visual guide to how the drop method works; usually you would specify the cleanup code that your type needs to run rather than a print message.