Internal arrays
Arrays are defined as data structures that have a known size at compile time. Rust takes this very seriously, and the array constructor will only take constants to denominate size in an array. [0u8; 4] will work, but let my_array_size = 2 * 2; [0u8; my_array_size] won't.
So, how do you dynamically reallocate a new array then? In Rust, there is also something called slices, which are views into a sequence data structure, akin to an array. These are a great fit when stored inside a Box pointer: allocated on the heap, it has all the benefits of an array with a dynamic size.
As previously mentioned, this implementation goes with Java's ArrayList growth strategy and increases its size by at least 50% each time more capacity is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access