Defining Enums: A Way to Enumerate Possibilities
In the previous series, we learned how to group related data together using structs. Now, we're going to look at another powerful way to define custom types in Rust: enums. An enum, or enumeration, allows you to define a type by enumerating its possible variants.
Generics in Structs and Enums
In our previous exploration of Generic Data Types, we saw how to reduce code duplication by creating generic functions. Now, we'll extend this powerful concept to our custom data types. This article dives into how to define and use generics in structs and enums, allowing us to build flexible, reusable data structures that are central to idiomatic Rust.
Pattern Matching with `match`
We've seen the match expression a few times already, especially when working with enums like Option and Result. In this article, we're going to take a deeper dive into what makes match so powerful: pattern matching.
The `Option` Enum: Handling the Absence of a Value
In the previous article, we learned how to define our own enums. Now, we're going to look at a very special and common enum from the Rust standard library: Option. The Option enum is Rust's answer to the problem of null values, which are a common source of bugs in many other programming languages.
The `Result` Enum and Error Handling
In the previous article, we learned how the Option enum is used to handle values that might be absent. Now, we'll look at another crucial enum from the standard library: Result. The Result enum is the primary way that Rust handles operations that can fail, such as reading a file or parsing a string.