capypad
0 day streak
rust / beginner
Snippet

Defining Basic Enums

Enums (enumerations) allow you to define a type by enumerating its possible variants. Variants can also hold data.

snippet.rs
rust
1
2
3
4
5
6
7
8
enum WebEvent {
PageLoad,
PageUnload,
KeyPress(char),
}
 
let load = WebEvent::PageLoad;
let press = WebEvent::KeyPress('x');
Breakdown
1
enum WebEvent { ... }
Declares an enum type named WebEvent with three possible variants.
2
KeyPress(char)
A variant that includes additional character data.
3
WebEvent::PageLoad
Accessing a specific variant using the double colon syntax.