Categorizing with Enumerations

Let's look at enumerations and match statements.

The bouncer would like even more functionality. He’d like to know how to treat different visitors and if they can drink alcohol. The final set of improvements to the visitor list are:

  • Store visitor’s action: Admit them, admit them with a note, refuse entry, or mark them as probationary treehouse members.

  • Store the visitor’s age, Forbid them from drinking if they’re under 21 (the legal drinking age in the United States).

Enumerations

Rust lets us define a type that can only be equal to one of a set of predefined values. These are known as Enumerations, and are declared with the enum keyword.

We’re going to define an enumeration listing each of the four actions associated with a visitor. Rust enumerations are very powerful, and can include data and even functions for each enumeration. Towards the top of the file outside of any functions, create a new enumeration with the following code:

Get hands-on with 1200+ tech skills courses.