Enums
An enum, also known as a sum type
, is a type that consists of several variants where each variant is named and has a type.
Let's take a look at an example where we define an enum called Color
with a few color variations.
enum Color {
Blue: (),
Green: (),
Red: (),
Silver: (),
Grey: (),
}
We begin by using the enum
keyword followed by the name for our enumeration. The variants are contained inside {}
and they are ordered sequentially from top to bottom. Each variant has a name, such as the first Blue
variant, and a type, which in this case is the unit type ()
for all variants.
The unit type is a type that does not contain any data however any type can be used.
// To instantiate an enum with a variant of the unit type the syntax is
let blue = Color::Blue;
let silver = Color::Silver;
Enums of Structs
In order to demonstrate more complex data types we can define a struct and assign that struct as a data type for any of an enum's variants.
Here we have a struct Item
and an enum MyEnum
. The enum has one variant by the name Product
and its type is declared to the right of :
which in this case is our struct Item
.
struct Item {
amount: u64,
id: u64,
price: u64,
}
enum MyEnum {
Product: Item,
}
fn main() {
let my_enum = MyEnum::Product(Item {
amount: 2,
id: 42,
price: 5,
});
}
Enums of Enums
Similar to structs we can use other enums as types for our variants.
enum UserError {
InsufficientPermissions: (),
Unauthorized: (),
}
enum Error {
UserError: UserError,
}
fn main() {
let my_enum = Error::UserError(UserError::Unauthorized);
}