liveBook · Manning

Chapter 6: More collections, more error handling

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

This chapter introduction previews additional Rust collections and later discussions of the ? operator, panic, and unwrap.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews additional Rust collections and later discussions of the ? operator, panic, and unwrap.

6.1 Other collections

Introduces where Rust’s other collection types are found and how to bring them into scope.

Key terms: std::collections, standard library, use statement

6.1.1 HashMap (and BTreeMap)

This section introduces HashMap key–value storage, contrasts its key order with BTreeMap, and shows ways to look up, replace, and update values.

Key terms: HashMap, key, value, HashMap::new(), .insert(), BTreeMap, keys, ordered, square brackets, .get()

6.1.2 HashSet and BTreeSet

This section explains how sets keep distinct values and how BTreeSet provides ordering that HashSet does not.

Key terms: HashSet, HashMap, keys, BTreeSet, ordering

6.1.3 BinaryHeap

This section introduces BinaryHeap as a collection that puts its greatest item first and illustrates its use for prioritizing tasks.

Key terms: BinaryHeap, priority queue, BinaryHeap<(u8, &str)>, u8, &str

6.1.4 VecDeque

Explains why removing items from the front of a Vec is costly and when a VecDeque is a better choice.

Key terms: Vec, .pop(), .remove(), remove(0), VecDeque, ring buffer, VecDeque::from()

6.2 The ? operator

This section explains how `?` handles `Result` by extracting successful values or returning errors early, then shows where that behavior is useful and where differing error types limit it.

Key terms: ? operator, Result, Ok, Err, early return, match, .parse(), ParseIntError, Result<i32, ParseIntError>, main()

6.3 When panic and unwrap are good

This section explains when deliberate panics help catch mistakes, how assertions and expect improve failure messages, and how unwrap_or avoids a panic by supplying a fallback value.

Key terms: panic!, logic bug, Vec, assert!, assert_eq!, assert_ne!, unwrap(), expect, None, unwrap_or()

6.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Generics, option, and result Chapter 7: Traits: Making different types do the same thing →

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.