TL;DR We explore the internal structure of Rust's Vec
A conspiracy!
As I was reading the Rust API documentation for std::vec::Vec, something interesting caught my eye in the Vec struct definition.
pub struct Vec < T , A = Global > where A : Allocator , { }
I am looking at you { /* private fields */ } ! "What are you trying to hide from me?" I thought. Was I being sucked into a grand conspiracy? The documentation gives no hints as to what these fields are other than giving us a visual representation of the data structure:
ptr len capacity +--------+--------+--------+ | 0x0123 | 2 | 4 | +--------+--------+--------+ | v Heap +--------+--------+--------+--------+ | 'a' | 'b' | uninit | uninit | +--------+--------+--------+--------+
Surely we will find that our Vec has three fields ptr len and capacity . But to be sure we have to go straight to the source. Are you ready to come with me down the rabbit hole and see if we can uncover an age-old mystery?
The many faces of Vec
Diving into the struct definition of Vec in std::vec this is what we find:
pub struct Vec < T , A : Allocator = Global > { buf : RawVec < T , A > , len : usize , }
... continue reading