liveBook · Manning

Chapter 15: Default, the builder pattern, and Deref

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

The introduction explains why builder-style method chaining and Deref can be useful for creating and extending types.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why builder-style method chaining and Deref can be useful for creating and extending types.

Key terms: builder pattern, chaining method after method, struct, Deref trait, methods

15.1 Implementing Default

This section shows how Default provides a sensible starting value for a type and why implementing the trait is useful beyond saving constructor arguments.

Key terms: Default trait, default(), base state, Default, trait, .unwrap_or_default(), new(), #[derive(Default)], f64, Size::default()

15.2 The builder pattern

Introduces the builder pattern as a readable way to start with default values and selectively customize a type, especially one with many fields.

Key terms: builder pattern, default values, chain method after method

15.2.1 Writing builder methods

This section shows how builder methods take ownership of a struct, change one field, and return the struct so method calls can be chained.

Key terms: mut self, Self, builder method, chain methods, Character

15.2.2 Adding a final check to the builder pattern

This section adds a final validation step to a Character builder and explains why a usability flag cannot enforce valid construction.

Key terms: builder pattern, .build(), Result, Ok(Self), u32, .to_lowercase(), can_use, Character

15.2.3 Making the builder pattern more rigorous

The section shows how a separate builder type can prevent unvalidated Character values and explains why builder methods help control how people use a type.

Key terms: CharacterBuilder, Character, .try_build(), builder pattern, Default

15.3 Deref and DerefMut

This section explains why a newtype wrapping a String may need Deref when its inner value cannot be accessed directly.

Key terms: Deref trait, newtype pattern, File, String, .0, mod

15.3.1 Deref basics

Introduces Deref as the trait behind * dereferencing and explains why a number-holding struct cannot be dereferenced until it implements the trait.

Key terms: Deref, dereference, reference, HoldsANumber, Box, pub, smart pointer, metadata, Vec, Rc

15.3.2 Implementing Deref

Shows how implementing Deref lets HoldsANumber expose its wrapped u8 through * and u8 methods, while noting that Deref alone does not allow mutation.

Key terms: Deref, HoldsANumber, u8, .checked_sub(), Some, None, mutable access, DerefMut

15.3.3 Implementing DerefMut

This subsection explains how DerefMut builds on Deref to provide mutable access, then shows why Deref can be useful for wrapper types.

Key terms: DerefMut, Deref, Self::Target, deref_mut(), type safety, Email, Quantity

15.3.4 Using Deref the wrong way

This section uses a deliberately confusing Character example to explain why Deref should generally be reserved for smart pointers.

Key terms: Deref, DerefMut, smart pointers, hit_points

15.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 14: Testing and building your code from tests Chapter 16: Const, “unsafe” Rust, and external crates →

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.