知识卡片

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

Box 提供独占堆存储

理解等级:中级

Box<T> 独占堆上的数据。在变量间移动它只移动指针一侧的表示,被指向的 T 就地保留。离开所有者作用域时箱内值随之清理。额外分配应服务于需要间接访问、控制表示大小或拥有 trait 对象的需求。

相关卡片 [[智能指针组合管理行为]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 智能指针 Using Box<T> to Point to Data on the Heap · 查看原书依据

      We’ll demonstrate the first situation in “Enabling Recursive Types with Boxes” later in this chapter. In the second case, transferring ownership of a large amount of data can take a long time because the data is copied around on the stack. To improve performance in this situation, we can store the large amount of data on the heap in a box.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0020) - 结论依据:原文用移动大数据时保持数据在堆中不动解释间接表示的用途。 - 原始内容:We’ll demonstrate the first situation in “Enabling Recursive Types with Boxes” later in this chapter. In the second case, transferring ownership of a large amount of data can take a long time because the data is copied around on the stack. To improve performance in this situation, we can store the large amount of data on the heap in a box.