Bits, Bytes, and Memory

The very first program we wrote, “Hello World!”, included strings. So, it’s a bit weird that we’re claiming to cover strings again. However, we’ve only covered the very basics of strings, as string literals, so far. Now that we know about ownership and references, it’s time to go into more depth.

The type of a number literal is i32. The type of a Boolean literal is bool. But the type of a string literal is &str. Now that we know about references, we can ask an important question: why do string literals need to be a reference when number and Boolean literals don’t?

A little bit of backstory before we dive in:

A bit is the most basic unit on a computer, and is either 0 or 1. When we put 8 bits together, we get a byte. We’ve used i32s a lot. Those take up 32 bits in memory, or 32 / 8 = 4 → 4 bytes.

For the moment, we’re going to assume that a single character, like the letter H in “Hello”, takes up 1 byte. The real story is a bit more complicated, but this will do for the moment.

In References, we mentioned that references hold addresses. Below, it’s going to be important to know how much memory an address takes up. It turns out that this depends on the machine we’re on. A 32-bit machine uses 32-bit addresses, and a 64-bit machine uses 64-bit addresses. The important thing is that, when compiling your program, the compiler knows which machine it’s compiling for, and knows the size of the addresses. For our purposes in this lesson, we’re going to pretend like addresses are always 64 bits, or 8 bytes.

Every piece of data we work with in our program has to be somewhere in memory. We’ve treated let x = 5 as meaning, “x has 5 somewhere.” For this lesson, it will be important to think about where.

OK, with that backstory, we can dive into what makes strings special.