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
  • More on shadowing
  • Copy types
  • More about printing

In this chapter, you’ll see how Rust keeps you thinking about the computer 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 this word, ownership—it’s probably Rust’s most unique idea. We’ll start with the two types of memory 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 in the last chapter. Look for that at the end!

2.1 The stack, the heap, pointers, and references

Understanding the stack, the heap, pointers, and references is very important in Rust. We’ll start with the stack and the heap, which are two places to keep memory in computers. Here are some important points to keep in mind:

2.2 Strings

2.3 const and static

2.4 More on references

2.5 Mutable references

2.5.1 Rust’s reference rules

2.5.2 Situation 1: Only one mutable reference

2.5.3 Situation 2: Only immutable references

2.5.4 Situation 3: The problem situation

sitemap