Box<T>

The Box<T> type for heap allocation.

Box<T>, casually referred to as a ‘box’, provides the simplest form of heap allocation in Rust. Boxes provide ownership for this allocation, and drop their contents when they go out of scope. Boxes also ensure that they never allocate more than isize::MAX bytes.

实现:在堆分配一个 T(大小不定),在栈分配一个 Box 管理 T(指向,读取,销毁)。T 的生命周期由 Box 管理,Box 的生命周期由栈帧管理。

标准库定义

pub struct Box<  
    T: ?Sized,  
    A: Allocator = Global,  
>(Unique<T>, A);

方法

.deref()

拿到 T 的不可变引用(一般自动操作)

.deref_mut()

拿到 T 的可变引用(一般自动操作)

*Box

内置的解引用