Module std::boxedStable [-] [+] [src]

A pointer 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 are useful in two situations: recursive data structures, and occasionally when returning data. The Pointer chapter of the Book explains these cases in detail.

Examples

Creating a box:

fn main() { let x = Box::new(5); }
let x = Box::new(5);

Creating a recursive data structure:

#[derive(Debug)] enum List<T> { Cons(T, Box<List<T>>), Nil, } fn main() { let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil)))); println!("{:?}", list); }
#[derive(Debug)]
enum List<T> {
    Cons(T, Box<List<T>>),
    Nil,
}

fn main() {
    let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
    println!("{:?}", list);
}

This will print Cons(1i32, Box(Cons(2i32, Box(Nil)))).

Structs

Box

A pointer type for heap allocation.

Statics

HEAP

A value that represents the heap. This is the default place that the box keyword allocates into when no place is supplied.

Traits

BoxAny

Extension methods for an owning Any trait object.

Functions

into_raw

Consumes the Box, returning the wrapped raw pointer.