知识卡片
引用不决定对象存放位置
引用可以指向栈上或堆上的值;Box拥有通常分配在堆上的对象,便于把所有权移出当前作用域。堆分配不等于永久存活:Box的拥有者最终析构时仍释放对象。指向Sized对象的地址示意也不能推广成所有引用都只有一个机器字,切片与trait对象还需元数据。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Rust References · 查看原书依据
stack or on the heap. Rust allocates items on the stack by default, but the Box<T> pointer type (roughly equivalent to C++’s std::unique_ptr<T>) forces allocation to occur on the heap, which in turn means that the allocated item can outlive the scope of the current block. Under the covers, Box<T> is also a simple eight-byte pointer value: let box_pt = Box::n
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Rust References》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u040-ch01-054)。
- 结论依据:原文在“stack or on the heap”附近的定义、例子或代码支撑本卡核心说明:引用可以指向栈上或堆上的值;Box<T>拥有通常分配在堆上的对象,便于把所有权移出当前作用域。边界按已读前提解释;示例未执行。
- 原始内容:stack or on the heap. Rust allocates items on the stack by default, but the Box<T> pointer type (roughly equivalent to C++’s std::unique_ptr<T>) forces allocation to occur on the heap, which in turn means that the allocated item can outlive the scope of the current block. Under the covers, Box<T> is also a simple eight-byte pointer value: let box_pt = Box::n