liveBook · Manning

Chapter 12: More on closures, generics, and threads

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

The introduction connects the chapter’s new tools to familiar Rust concepts and gives a few high-level distinctions.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction connects the chapter’s new tools to familiar Rust concepts and gives a few high-level distinctions.

Key terms: impl Trait, generics, Arc, Rc, thread-safe, scoped threads, scope, channels, messages

12.1 Closures as arguments

Introduces how function signatures specify closure arguments and uses examples to show when a closure can be called again.

Key terms: Fn, FnMut, FnOnce, F, Self::Item, trait bounds, some_vec, .into_iter(), by value, by reference

12.1.1 Some simple closures

This section uses simple functions to show how often Fn, FnMut, and FnOnce closures can be called, and how closures are passed as arguments.

Key terms: Fn(), FnMut(), FnOnce(), capture variables, environment, arguments, closure, argument, inline

12.1.2 The relationship between FnOnce, FnMut, and Fn

Explains the supertrait relationship among Rust’s three closure traits and how it determines which closures a function can accept.

Key terms: FnOnce, FnMut, Fn, supertrait, subtrait, closure trait

12.1.3 Closures are all unique

This section explains why two closures with identical signatures still have different types and how to accept both in one function.

Key terms: closure, signature, Fn, trait, F, G

12.1.4 A closure example

This section uses a City example to show how an FnMut closure can change stored data without taking ownership of it.

Key terms: City, change_city_data, FnMut, Vec<u32>

12.2 impl Trait

The section briefly reviews how familiar generics use a type placeholder before introducing impl Trait as another form of generics.

Key terms: generics, T

12.2.1 Regular generics compared to impl Trait

This section compares regular generic parameters with impl Trait parameters, explains why impl Trait cannot express a shared type for the comparison example, and identifies where impl Trait can be used.

Key terms: generics, impl Trait, PartialOrd, Display, T, impl PartialOrd + Display, return type, closures, type alias

12.2.2 Returning closures with impl Trait

This section explains how a function can return a closure using impl Trait and how the caller can use that closure.

Key terms: impl Trait, closure signature, FnMut(i32) -> i32

12.3 Arc

This section explains why shared ownership across threads uses Arc, why changing shared data also requires Mutex, and how to use that combination with multiple threads.

Key terms: Arc, Rc, atomic reference counter, atomic operations, thread-safe, Mutex, Arc<Mutex<i32>>, clone, move, handles

12.4 Scoped threads

This section explains how scoped threads simplify short-lived concurrent work by allowing borrowed data and automatically joining threads at the end of a scope.

Key terms: scoped threads, non-'static data, .join(), thread::scope(), thread::spawn(), scope, Mutex, Arc, move, mutable borrow

12.5 Channels

Introduces channels as a simple, thread-safe way to send information to one receiver, even across threads or different parts of a program.

Key terms: channel, sender, receiver, thread-safe

12.5.1 Channel basics

This section introduces Rust’s mpsc channel as a paired sender and receiver and shows how to create the pair while letting Rust infer its message type.

Key terms: std::sync::mpsc, mpsc, channel(), Sender, Receiver, tuple, destructuring, generic, type inference

12.5.2 Implementing a channel

Shows how to send values through a channel, receive them across threads, and understand when channel operations wait or fail.

Key terms: Sender, Receiver, Result, send(), clone, recv(), try_recv(), blocking, Ok, Err

12.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 11: Multiple threads and a lot more Chapter 13: Box and Rust documentation →

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.