liveBook · Manning

Chapter 13: Box and Rust documentation

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

The introduction explains why Rust documentation is transferable to read, why Box matters, and how to recognize attributes.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why Rust documentation is transferable to read, why Box matters, and how to recognize attributes.

Key terms: documentation, Box, smart pointer, traits, attributes, #[derive(Debug)]

13.1 Reading Rust documentation

This section explains why Rust documentation is useful to read and how to open documentation for your own code.

Key terms: Rust documentation, cargo doc, standard library

13.1.1 assert_eq!

Explains how to read assert_eq! in Rust documentation and why documentation examples use it.

Key terms: assert_eq!, panic, println!, Display, Debug

13.1.2 Searching

This section shows how to search Rust documentation quickly, even after scrolling down a page.

Key terms: search bar, S key

13.1.3 The [src] button

Shows when to open Rust documentation’s [src] view and how source code can reveal implementation details and method requirements.

Key terms: [src], source code, String, with_capacity(), Cell, Copy, impl<T: Copy>, .get(), .clone()

13.1.4 Information on traits

This section shows how to read a trait’s required methods in Rust documentation and when deriving an implementation can replace writing a method yourself.

Key terms: Required Methods, Iterator, From, Debug, #[derive(Debug)]

13.1.5 Attributes

This section explains how Rust attributes affect code and compiler behavior, using common attributes for warnings, traits, configuration, and API changes.

Key terms: attribute, outer attribute, inner attribute, #[allow(dead_code)], #[allow(unused_variables)], #![allow(unused)], #[derive(TraitName)], Display, Copy, Clone

13.2 Box

Introduces Box as a pointer type that puts a value’s data on the heap and shows how to create one.

Key terms: Box, pointer, heap, stack, Box::new()

13.2.1 Some Box basics

Introduces Box as an owning smart pointer and shows how its known size makes a recursive struct possible.

Key terms: Box, smart pointer, reference, *, recursive, Holder, Box::new(), Some, None

13.2.2 Putting a Box around traits

Explains why boxing a trait value lets a function return values of different sizes, and contrasts dynamic dispatch with the static dispatch used for generics.

Key terms: Box, trait, size, dyn, dynamic dispatch, static dispatch, generics

13.2.3 Using a Box to handle multiple error types

This section shows why Box<dyn Error> can hold different error types, what information a trait object hides, and why recovering a concrete error type can matter.

Key terms: std::error::Error, Debug, Display, Box<dyn Error>, trait object, concrete type, type erasure, downcasting, dyn Error

13.2.4 Downcasting to a concrete type

This section shows how to recover a concrete error type from a boxed Error trait object by downcasting.

Key terms: Box<dyn Error>, downcast, downcast_ref, concrete type

13.3 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 12: More on closures, generics, and threads Chapter 14: Testing and building your code from tests →

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.