liveBook · Manning

Chapter 2: Memory, variables, and ownership

What you will learn in chapter 2 of Learn Rust in a Month of Lunches

Introduces Rust’s focus on memory use and ownership without yet explaining the memory mechanisms.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces Rust’s focus on memory use and ownership without yet explaining the memory mechanisms.

Key terms: memory, ownership

2.1 The stack, the heap, pointers, and references

Explains why Rust uses the stack, heap, and pointers, then introduces references as borrowed access to another value’s memory.

Key terms: stack, heap, pointer, compile time, reference, borrow, own, &

2.2 Strings

This section distinguishes borrowed string slices from owned strings and shows how Rust represents and creates them.

Key terms: String, &str, string slice, owned, str, dynamically sized, reference, stack, UTF-8, Unicode

2.3 const and static

Introduces const and static as typed, usually global declarations, explains how they differ, and outlines when they can be used.

Key terms: const, static, let, type inference, fixed memory location, global, NUMBER_OF_MONTHS, dropped, compile time, heap

2.4 More on references

This section shows that multiple references can view an owned String, but a function cannot return a reference to a String that disappears when the function ends.

Key terms: String, &String, reference, owns its data, ownership, code block

2.5 Mutable references

This section introduces mutable references and shows how dereferencing lets you change the value they refer to.

Key terms: mutable reference, &mut, &mut i32, dereferencing, *, referencing, &

2.5.1 Rust’s reference rules

States Rust’s rules for immutable and mutable references and explains why simultaneous reading and changing could cause problems.

Key terms: immutable references, mutable references, viewing data, change the data

2.5.2 Situation 1: Only one mutable reference

Uses a shared-presentation analogy to show why one mutable reference can safely make changes when nobody else is viewing the data.

Key terms: mutable reference, own the data

2.5.3 Situation 2: Only immutable references

This subsection uses a presentation analogy to explain why many immutable references can exist at the same time.

Key terms: immutable reference, data

2.5.4 Situation 3: The problem situation

This section explains why an active mutable borrow cannot overlap with an immutable borrow, and why code can work when the mutable borrow ends before the immutable one is used.

Key terms: mutable reference, immutable borrow, mutable borrow, immutable reference, number_change

2.6 Shadowing again

This section uses a reference to show that shadowing blocks access to an earlier binding without destroying its value.

Key terms: shadowing, reference, String, i32

2.7 Giving references to functions

This section explains how passing a String to a function transfers ownership, while passing a reference lets the function use the String without owning it.

Key terms: owner, move, use of moved value, String, &String, &mut String, borrows, reference, mut string_to_add_hungary_to, mut country: String

2.8 Copy types

This section explains why Copy values are duplicated automatically, how to check whether a type is Copy, and when to use cloning or references instead.

Key terms: Copy types, move semantics, copy semantics, String, Trait Implementations, Copy, Clone, .clone(), immutable reference, &

2.9 Variables without values

This section explains how to declare a variable before assigning it a value, and why that can be useful when the value comes from a code block.

Key terms: uninitialized variable, let, initialized, code block, my_number, mut

2.10 More about printing

This section explains ways to print strings, bytes, addresses, numbers, and formatted values in Rust, then reinforces ownership with a computer-sharing analogy.

Key terms: escape, raw, r#, bytes, ASCII, b, Unicode escape, \u{}, {:p}, pointer address

2.11 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: Some basics Chapter 3: More complex types →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.