Rust Box
Box< T >
- A smart pointer called Box<T> points to the data that is allocated on the type T heap. You can store the data on the heap instead of the stack by using Box<T>.
- An owned pointer is Box<T>.
- Apart from storing the data on the heap, boxes have no performance overhead.
- The destructor is called to eliminate all internal objects and free up memory when the Box exits the scope.
Using Box to store the data on the heap.
The data is mostly stored on the heap using Box<T>.
Example:
fn main()
{
let a = Box :: new(1);
print!("value of a is : {}",a);
}
Output:
value of a is : 1
The value of Box, which in the case above points to data 1, is included in a. Should we retrieve the value of Box, the software outputs ‘1’. The Box is deallocated upon the program’s termination. The data that the box points to is kept on the heap, and the box itself is kept on the stack.
Example:
Cons List
- Cons stand for “Construct function”
- A new pair, known as a List, is created from the two arguments using a data structure called a cons list.
- If we have two items, x and y, the cons function cons the “x onto y” indicates that we build the new container with x as the first element and y as the second.
- The current item and the last item make up the two elements of the cons list. Since Nil does not include the next item, it is the final item on the cons list.
The enum containing the list of cons is now created.
enum List
{
cons(i32, List),
Nil,
}
The enum of List type that holds the cons list data structure of i32 values is created in the code above.
Example:
enum List {
Cons(i32, List),
Nil,
}
use List::{Cons, Nil};
fn main()
{
let list = List::Cons(1,Cons(2,Cons(3,Nil)));
for i in list.iter()
{
print!("{}",i);
}
}
Output:
The Rust compiler issues an error stating that the list type “has infinite size” in the example above since it has a recursive variation. Rust is unable to determine how much space is needed to store the List value as a result. The Box<T> can be used to solve the infinite size problem.
Using Box< T > to get the size of a recursive type
Rust is unable to determine how much storage space is needed for the recursive data types. The error from the prior instance is displayed by the Rust compiler:
= help: insert indirection (e.g., a 'Box', 'Rc', or '&') at some point to make 'List' representable
Since the compiler is aware of the space required by the Box<T> pointer, we can utilize it in the example above. A program’s execution will not alter the size of the Box<T> pointer. Instead of being saved in the cons variant, the List value that will be placed on the heap is indicated by the Box<T> reference. It is possible to insert the Box<T> pointer straight into the cons variant.
Example:
#[derive(Debug)]
enum List {
Cons(i32, Box),
Nil,
}
use List::{Cons, Nil};
fn main()
{
let list = Cons(1,Box::new(Cons(2,Box::new(Cons(3,Box::new(Nil))))));
print!("{:?}",list);
}
Output:
Cons(1, Cons(2, Cons(3, Nil)))
Note: If we use the Box<T> pointer in recursive data types, then the size of the List value will be equal to the size of i32 value plus the size of the box pointer?s data.