Program Memory

I’ve talked about the compiler a lot. So far, your interaction with it has been that, each time you hit “run” in the Rust Playground, the compiler checks to make sure your code is correct. If your code is wrong, it yells at you.

However, I haven’t told you much about what happens if your code is correct. The compiler will turn your correct code into an executable. The executable contains a list of instructions to your computer, like “add these two numbers” and “check if this number is bigger than this number.” This executable can be run, which will load all of those instructions into memory and start executing them.

But there’s more to your executable than just instructions. Your executable can also have additional data included. And that’s where your string literals live: in program memory itself! Then, the compiler creates a reference to that piece of memory, and that’s what ends up living on the stack, inside the &str values.

A str is known more formally as a string slice. A &str is technically a reference to a string slice. Though Rustaceans will often be sloppy with both of these terms, and refer to &str as “string slices” or even just “strings.”

Get hands-on with 1200+ tech skills courses.