April 2026
Intermediate
631 pages
16h 20m
English
Building upon the previous section on structs, let’s now turn our attention to enums, another powerful feature in Rust for defining types that can represent one of several possible variants. In this section, we’ll explore why enums are useful, show you how to define and enhance them with implementation blocks, and then consider adding data to enum variants to create even more flexible and expressive types.
Consider a situation where you must define a mutable variable to store day-of-the-week information. You could simply create a variable and assign it the desired day as a string, as follows:
let mut day = "Monday".to_string();
While this approach works, it’s far from ideal. The set of weekdays is limited, ...
Read now
Unlock full access