Enums and Pattern Matching - The Rust Programming Language
In this chapter we’ll look at enumerations, also referred to as enums. Enums allow you to define a type by enumerating its possible variants. First, we’ll define and use an enum to show how an enum can encode meaning along with data. Next, we’ll explore a particularly useful enum, called Option, which expresses that a value can be either something or nothing. Then we’ll look at how pattern matching in the match expression makes it easy to run different code for different values of an enum. Finally, we’ll cover how the if let construct is another convenient and concise idiom available to handle enums in your code.
Keyboard shortcuts Press ← or → to navigate between chapters Press S or / to search in the book Press ? to show this help Press Esc to hide this help Enums and Pattern Matching In this chapter, we’ll look at enumerations, also referred to as enums. Enums allow you to define a type by enumerating its possible variants. First we’ll define and use an enum to show how an enum can encode meaning along with data. Next, we’ll explore a particularly useful enum, called Option, which expresses that a value can be either something or nothing. Then, we’ll look at how pattern matching in the match…
saved by
related reading
- The match Control Flow Construct - The Rust Programming Languagedoc.rust-lang.org
- Defining an Enum - The Rust Programming Languagedoc.rust-lang.org
- Control Flow - The Rust Programming Languagedoc.rust-lang.org
- Using Structs to Structure Related Datadoc.rust-lang.org
- Generic Data Types - The Rust Programming Languagedoc.rust-lang.org
- Programming a Guessing Game - The Rust Programming Languagedoc.rust-lang.org
- An Example Program Using Structs - The Rust Programming Languagedoc.rust-lang.org
- Generic Types, Traits, and Lifetimes - The Rust Programming Languagedoc.rust-lang.org
- Rust Iterator Cheat Sheetdanielkeep.github.io
- A half-hour to learn Rustfasterthanli.me
- Variables and Mutability - The Rust Programming Languagedoc.rust-lang.org
- Methods - The Rust Programming Languagedoc.rust-lang.org