Enums
The enum keyword allows the creation of a type which may be one of a few
different variants. Any variant which is valid as a struct is also valid in
an enum.
// Create an `enum` to classify a web event. Note how both
// names and type information together specify the variant:
// `PageLoad != PageUnload` and `KeyPress(felt252) != Paste(ByteArray)`.
// Each is different and independent.
#[derive(Drop)]
enum WebEvent {
// An `enum` variant may either be `unit-like`,
PageLoad,
PageUnload,
// like tuple structs,
KeyPress: felt252,
Paste: ByteArray,
// or c-like structures.
Click: Point,
}
#[derive(Drop)]
struct Point {
x: u64,
y: u64,
}
// A function which takes a `WebEvent` enum as an argument and
// returns nothing.
fn inspect(event: WebEvent) {
match event {
WebEvent::PageLoad => println!("page loaded"),
WebEvent::PageUnload => println!("page unloaded"),
// Destructure `c` from inside the `enum` variant.
WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
WebEvent::Paste(s) => println!("pasted \"{}\".", s),
// Destructure `Click` into `x` and `y`.
WebEvent::Click(Point { x, y }) => { println!("clicked at x={}, y={}.", x, y); },
}
}
fn main() {
let pressed = WebEvent::KeyPress('x');
let pasted = WebEvent::Paste("my text");
let click = WebEvent::Click(Point { x: 20, y: 80 });
let load = WebEvent::PageLoad;
let unload = WebEvent::PageUnload;
inspect(pressed);
inspect(pasted);
inspect(click);
inspect(load);
inspect(unload);
}
Type aliases
If you use a type alias, you can refer to each enum variant via its alias. This might be useful if the enum's name is too long or too generic, and you want to rename it.