Enum
Syntax
Enumeration :
enum
IDENTIFIER{
EnumField*
EnumMethod*
}
EnumField :
IDENTIFIER | IDENTIFIER(
TupleElements?)
EnumMethod :
Function
An enum, also referred to as enumeration is a simultaneous definition of a nominal Enum type, that can be used to create or pattern-match values of the corresponding type.
Enumerations are declared with the keyword enum
.
An example of an enum
item and its use:
enum Animal {
Dog
Cat
Bird(BirdType)
pub fn bark(self) -> String<10> {
match self {
Animal::Dog => {
return "bow"
}
Animal::Cat => {
return "meow"
}
Animal::Bird(BirdType::Duck) => {
return "quack"
}
Animal::Bird(BirdType::Owl) => {
return "hoot"
}
}
}
}
enum BirdType {
Duck
Owl
}
fn f() {
let barker: Animal = Animal::Dog
barker.bark()
}