Box<T>
Last updated
Last updated
Box<T>
Smart Pointer?π‘ Box<T>
is a smart pointer which allows you to allocate memory in the heap rather than the stack for data of type T
and ensures automatic deallocation when it goes out of scope.
Itβs used most often for these situations:
When the size of your data is unknown at compile time, and you can not allocate it on the stack.
When building recursive data structures like trees requires nodes that hold references to themselves.
When you need to transfer ownership of data allocated on the heap. By moving a Box<T>
, you transfer ownership of the underlying data as well.
When working with trait objects (dynamically sized types), you might need to allocate them on the heap.
Box<T>
We define the variable x
to have the value of a Box
that points to the value 10
, which is allocated on the heap. This program will print Value in the box: 10
.
When a box goes out of scope, x
will be deallocated, and this will happen for both the box which is stored on the stack, and its data which is stored in the heap as well.
Box<T>
As we all know Rust requires knowing the size of a type at compile time. This can be a challenge when dealing with recursive data structures, where a value can contain a reference to itself. Here's how Box<T>
comes into play to enable recursive types:
This code defines a List
enum with two variants: Cons
and Nil
.
Cons
holds an i32
value and a reference to another List
element. However, this creates a problem. The compiler cannot determine the size of List
at compile time because it depends on itself!
Instead, we're going to use Box<T>
to rewrite the List enum.
In this code, Cons
now holds an i32
value and a Box<List>
. This Box<List>
acts as a pointer to a List
element on the heap.
The compiler now knows the size of Cons
which holds i32
, and a pointer
, both with fixed sizes. Rust always knows how much space a Box<T>
needs because the size of the pointer doesnβt change based on the amount of data itβs pointing to.