The Rust Programming Language — Collections — Memory
Memory in computer science is nothing but series of bits. A bit is nothing but a flag 1 or 0. The smallest series of bits is known as byte which is continuous series of 8 bits. A memory in your computer is a big long bunch of 0 and 1.
But, standalone, 0 and 1 are not that useful. We are talking about how do we make meaning out of strings and numbers that we store in the memory. How do we make sense of 0 and 1 from the long series of bits in the memory?

The program that we are running is interpreting the bits in group of 8 (aka byte). So we have Byte 1 which is 11110011 and Byte 2 as 01101011. Byte is the smallest unit which is 8 bits series.
11110011 translates to 243 in binary.
01101011 translates to 107 in binary.

We won’t go into details of how binary is translated to numbers, but you can try it here and read the formula. Important part is we chose to interpret this memory in different ways in order to get more useful values. We can chose to interpret first 16 bits (i.e. 2 Bytes) to get some useful values.

So you can kind of think of these bytes representation of memory as a u8 array or u16 array.

Or you can treat them as u16 array.

Let’s assume that you create an array of 3 u16 elements, a tuple of 3 u16 elements and a struct of 3 u16 elements. Assume that all the 3 elements are same. Array, tuples and structs are represented the exact same way in memory in exact sequence without any extra bits by Rust. This is what makes Rust so powerful and performant. It gives you 3 syntactical sugars of collection which can be stored in the memory with exact same sequence of bits and also give you extra functionality for the language to work with. All 3 of the collections have exact same memory footprint and representation without additional overhead. For array, tuple or struct, it’s just the number in the memory.
Rust wants to have as little overhead as possible in the memory and give away no extra bit so it can be fast. This is how Rust can be faster than other languages. Rust gives you these nice features during compile time but at run time it saying we want to have no overhead in memory.
That’s it for Memory in The Rust Programming Language. Please share your feedback in the comments section.
You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.






