5 Generics, option, and result
This chapter covers
- Generics—when to use more than one type
Option
—when an operation might produce a value but might notResult
—when an operation might succeed but might not
Rust is a strict language with concrete types, but after this chapter, you’ll have three important tools to work with. Generics let you describe to Rust “some sort of type” that Rust will turn into a concrete type without you having to do it. After that, we’ll learn about two interesting enums called Option
and Result
. Option
tells Rust what to do when there might not be a value, and Result
tells Rust what to do when something might go wrong.
5.1 Generics
We’ve known since chapter 1 that Rust needs to know the concrete type for the input and ...
Get Learn Rust in a Month of Lunches now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.