From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Box data type

Box data type - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Box data type

- [Instructor] The Rust Standard Library includes several data types that use generics as part of their type definition. And the first example of this we'll look at is the Box Data Type. Box allows you to store data of some generic type T on the heap. Even if T is a data type that would normally be stored on the stack. The box consists of a pointer on the stack which points to a chunk of memory on the heap. That's been allocated large enough to hold whatever generic type T is. Boxes are considered a Smart Pointer, because they provide some additional functionality beyond what a regular reference can do. Beyond just simply pointing to a memory location. For example, unlike a borrowed reference, a box has ownership of the data it points to. And when the box goes out of scope it will automatically drop and deallocate the memory it was using on the heap. Rust has several other types of smart pointers with even more…

Contents