From the course: Rust Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Define enums

Define enums

- [Instructor] In Rust an enum, which is short for enumeration is a data type with multiple possible variants. It enumerates a finite number of options or types of something. In this chapter, we'll look at how to define custom enum types, how enums are commonly used and a couple of standard enums you'll likely encounter when programming with Rust. I wish I could keep the space theme going here but to demonstrate enums, I think shapes make for an easy to understand example. So let's define an enum to represent one of three possible shapes. To do that, we'll use the keyword enum followed by the name for the new enum type, shape and then a pair of curly braces. Within that code block, we'll list out or enumerate the possible shapes we want to represent, starting with a circle, a rectangle and a triangle. This defines a new data type that can be one of three possible variants. Now let's instantiate a new…

Contents