chapter two

2 Memory, variables, and ownership

 

This chapter covers

  • The stack, the heap, pointers, and references
  • Strings - The most common way to work with text
  • Const and static - Variables that last forever
  • Shadowing - When you give a variable the same name as another
  • Copy types
  • More about printing

In this chapter too you’ll see how Rust keeps you thinking about the computer system itself. Rust keeps you focused on how the computer's memory is being used for your program, and what ownership is (who owns the data). Remember the word “ownership” - it’s probably Rust’s most unique idea. We'll start with the two types of memory that a computer uses: the stack and the heap.

Oh, and there's quite a bit more to learn about printing to build on what you learned last chapter. Look for that at the end!

2.1 The stack, the heap, and pointers

The stack, the heap, and pointers, are very important in Rust.

The stack and the heap are two places to keep memory in computers. The important differences are:

2.2 Strings

2.3 const and static

2.4 More on references

2.5 Mutable references

2.5.1 Situation one: only one mutable reference

2.5.2 Situation two: only immutable references

2.5.3 Situation three: the problem situation

2.6 Shadowing again

2.7 Giving references to functions

2.8 Copy types

2.9 Variables without values

2.10 More about printing

2.11 Summary