liveBook · Manning

Chapter 21: Continuing the tour

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

Introduces the second standard-library tour and previews its topics, including macros.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the second standard-library tour and previews its topics, including macros.

21.1 std::mem

The section introduces useful std::mem functions and explains how drop, swap, replace, and take affect values.

Key terms: drop(), Copy, std::mem, swap(), mutable reference, replace(), let binding, take(), Default, Some

21.2 Setting panic hooks

This section explains how Rust handles panics, how to customize panic reporting with hooks, and why hook code must avoid panicking.

Key terms: panic, unwinds the stack, main thread, backtrace, std::panic, set_hook(), global panic hook, closure, PanicInfo, payload

21.3 Viewing backtraces

This section shows how to capture a backtrace at run time, enable it with an environment variable, inspect its status, and decide when it may be useful.

Key terms: std::backtrace, Backtrace::capture(), RUST_BACKTRACE, RUST_LIB_BACKTRACE, Backtrace::status(), BacktraceStatus, #[non_exhaustive], Unsupported, backtraces, stack traces

21.4 The standard library prelude

This section shows how the standard library prelude makes common Rust items available automatically and explains why older code may contain `extern crate`.

Key terms: prelude, #![no_implicit_prelude], Vec, Ok, extern crate, use, external crate

21.5 Other macros

Introduces a look at more standard-library macros before the next chapter turns to writing macros.

21.5.1 unreachable!

This section explains when to use Rust’s unreachable! macro and how an unreachable-pattern warning can reveal a mistake in a match.

Key terms: unreachable!, todo!, panic, unreachable pattern, summer, match

21.5.2 column!, line!, file!, and module_path!

This section introduces four macros that report where they are called in the code and shows why that location can be useful in a warning.

Key terms: column!, line!, file!, module_path!

21.5.3 thread_local!

This section explains how thread_local! creates a separately initialized static value for each thread and how code accesses that value.

Key terms: thread_local!, lazy_static!, LOCAL_INITIAL_VALUE, INITIAL_VALUE, .with(), closure, LocalKey

21.5.4 cfg!

The section shows how cfg! returns a boolean for configuration-dependent behavior, using a function that handles real files during a normal run and dummy data during a test.

Key terms: cfg!, configuration, bool, attributes, UserFile, File, String, open_file(), cargo run, cargo test

21.6 Summary

Concepts explained in this chapter

← Chapter 20: A tour of the standard library Chapter 22: Writing your own macros →

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.