liveBook · Manning

Chapter 5: Generics, option, and result

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

The chapter introduces generics, Option, and Result as tools for working with types, possibly missing values, and operations that might fail.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces generics, Option, and Result as tools for working with types, possibly missing values, and operations that might fail.

Key terms: Generics, concrete type, Option, Result, enums

5.1 Generics

This section shows how generics let functions work with different types, how trait bounds limit those types, and how to use multiple generic type names.

Key terms: generics, concrete type, <T>, generic over, Debug, {:?}, trait, generic type, T, U

5.2 Option and Result

This section distinguishes Option, Result, and ordinary return values by whether a value may be absent or an operation may fail.

Key terms: Option, Result

5.2.1 Option

Explains how Option represents a value that may be absent and how to handle its two cases without accidentally panicking.

Key terms: Option, Some, None, Option<i32>, enum, generic, T, match, pattern matching, .unwrap()

5.2.2 Result

This section explains how Result represents success or failure, how its values and errors are typed, and why errors should be handled rather than blindly unwrapped.

Key terms: Result, Option, Ok, Err, Some, None, Result<T, E>, Error, FromUtf8Error, .unwrap()

5.2.3 Some other ways to do pattern matching

The section introduces shorter pattern-matching forms for cases where only a matching value matters, then explains the parsing and vector methods used in a while-let example.

Key terms: if let, Some, None, Option, .get(), pattern match, let else, diverging code, continue, break

5.3 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Building your own types Chapter 6: More collections, more error handling →

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.