liveBook · Manning

Chapter 4: Lifetimes, ownership, and borrowing

What you will learn in chapter 4 of Rust in Action: Systems programming concepts and techniques

The introduction explains why Rust’s borrow checker matters and gives basic meanings for ownership, lifetimes, and borrowing.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why Rust’s borrow checker matters and gives basic meanings for ownership, lifetimes, and borrowing.

Key terms: borrow checker, ownership, owner, lifetime, borrowing

4.1 Implementing a mock CubeSat ground station

This section introduces a simplified CubeSat program that will serve as the chapter’s working example.

Key terms: CubeSat, ground station, constellation, check_status(), enum, stub function

4.1.1 Encountering our first lifetime issue

A CubeSat example shows that passing a value to a function can move its ownership, ending its lifetime even while its variable remains in scope.

Key terms: move, ownership, CubeSat, check_status(), lifetime, scope, drops, borrow checker, rebinding, live borrows

4.1.2 Special behavior of primitive types

This subsection explains why primitive values remain usable after being passed to a function while values of types without Copy may not.

Key terms: Copy trait, copy semantics, move semantics, primitive types

4.2 Guide to the figures in this chapter

4.3 What is an owner? Does it have any responsibilities?

This section explains that Rust ownership determines when values are cleaned up, while giving owners no special control over access or borrowing.

Key terms: owner, lifetime, destructor, Drop, drop(&mut self), custom destructor, references, ownership, borrowing

4.4 How ownership moves

This section shows how ownership of a CubeSat value moves through assignment, function arguments, and return values.

Key terms: ownership, assignment, function barrier, argument, return value

4.5 Resolving ownership issues

Introduces a CubeSat messaging example in which moving a satellite into a send call causes an ownership problem, then sets up types for the examples that follow.

Key terms: ownership, sat_a, base.send(), CubeSat, Mailbox, Vec<Message>, Message, String

4.5.1 Use references where full ownership is not required

This section shows how borrowing a CubeSat instead of taking ownership lets ground-station methods access and modify it, while a sent message moves into its mailbox.

Key terms: borrow, ownership, & T, &mut T, GroundStation.send(), CubeSat.recv(), CubeSat.messages, messages.push(), mutable access

4.5.2 Use fewer long-lived values

This section resolves ownership difficulties in the CubeSat example by creating short-lived satellite objects and keeping their messages in a separate, long-lived mailbox.

Key terms: short-lived variables, CubeSat, ownership, Mailbox, Vec<Message>, sender, recipient, mutable borrow, message vector

4.5.3 Duplicate the value

This section presents duplication as an alternative to restructuring ownership and distinguishes Rust’s implicit Copy behavior from explicit Clone calls.

Key terms: Copy, Clone, .clone(), ownership, String, std::rc::Rc<T>, derive, impl blocks

4.5.4 Wrap data within specialty types

This section shows how wrapper types provide shared ownership and, when needed, mutation, while adding runtime costs and limits.

Key terms: Rc<T>, shared ownership, reference counting, Clone, Drop, Rc<RefCell<T>>, interior mutability, immutable façade, shared read/write access, runtime cost

4.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Compound data types Chapter 5: Data in depth →

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.