liveBook · Manning

Chapter 11: Multiple threads and a lot more

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

This chapter introduction previews advanced Rust topics, including type aliases, Cow, Rc, and multiple threads, without explaining them in depth.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews advanced Rust topics, including type aliases, Cow, Rc, and multiple threads, without explaining them in depth.

11.1 Importing and renaming inside a function

This section shows how to place imports inside a function, use wildcard imports cautiously, and rename imported items with `as`.

Key terms: use, MapDirection, North, NorthEast, ::*, prelude, as, OtherDirectory, FileState, SimilarFileNameInNextDirectory

11.2 The todo! macro

This section shows how todo! lets you compile a prototype with unfinished code, while explaining its limits and what happens if execution reaches it.

Key terms: todo!, prototyping, panic, signatures, undeclared types, struct parameters

11.3 Type aliases

This section explains how type aliases give existing types clearer or shorter names without creating new types.

Key terms: type alias, import items, orphan rule, String

11.4 Cow

Introduces Cow as a way to hold either a borrowed value or an owned value, and shows how that choice applies to strings, functions, and structs.

Key terms: Cow, clone on write, Borrowed, Owned, Cow::Borrowed, format!(), .into(), 'a, ToOwned, ?Sized

11.5 Rc

Introduces Rc as a way to share ownership while respecting Rust’s ownership rules.

Key terms: Rc, reference counter, reference counted, shared ownership

11.5.1 Why Rc exists

This section explains why Rc is useful when sharing ownership is preferable to cloning, and compares its ownership tracking with garbage collection.

Key terms: Rc, owner, clone, dropped, garbage collection, references, lifetimes

11.5.2 Using Rc in practice

The section shows how to share data with Rc, count its owners, and use weak references to avoid reference cycles.

Key terms: Rc, Rc::clone(&item), Rc<String>, strong_count(), strong pointers, City, CityData, weak references, reference cycle, Rc::downgrade(&item)

11.5.3 Avoiding lifetime annotations with Rc

This section shows how sharing city names with Rc<String> avoids repeating lifetime annotations through nested structs without cloning each String.

Key terms: lifetime, City, Country, World, Vec<City>, Vec<Country>, Rc<String>, Vec<Rc<String>>, clone the Rc, lifetime annotations

11.6 Multiple threads

This section introduces how Rust uses OS threads to do multiple things at the same time.

Key terms: multiple threads, OS threads, state stack, local state

11.6.1 Spawning threads

This section shows how to spawn threads with closures and explains why their output may be unpredictable when the main thread finishes first.

Key terms: std::thread::spawn, closure, thread, main thread, main(), panic, JoinHandle, 'static lifetime, return value

11.6.2 Using JoinHandles to wait for threads to finish

The section shows how to use JoinHandles to keep main() running until spawned threads finish, and why joining after spawning every thread differs from joining each one immediately.

Key terms: JoinHandle, spawn(), .join(), main(), Vec, JoinHandles

11.6.3 Types of closures

Introduces the three closure traits through how a closure captures variables and explains why taking a value can limit repeated calls.

Key terms: closure, Fn, FnMut, FnOnce, takes by value, mutable reference

11.6.4 Using the move keyword

Explains why a spawned thread needs a closure to own values it uses from outside, and how `move` makes that happen.

Key terms: move, spawn(), Fn, FnOnce, borrow, take by value

11.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 10: Lifetimes and interior mutability Chapter 12: More on closures, generics, and threads →

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.