enum Color {
red
green
blue
}
enum HttpStatus {
ok = 200
not_found = 404
error = 500
}
@[flag]
enum Permission {
read
write
execute
}
fn main() {
mut c := Color.red
c = .green // shorthand when the type is known
println(c) // green
println(int(HttpStatus.not_found)) // 404
match c {
.red { println('R') }
.green { println('G') }
.blue { println('B') }
}
status := HttpStatus.from(404) or { HttpStatus.error } // int → enum
color := Color.from_string('blue') or { Color.red } // newer versions
mut perms := Permission.read | Permission.write // bit flags
println(perms.has(.write))
perms.set(.execute)
perms.clear(.read)
println(perms.all(.write | .execute))
}
// Enums can have methods
fn (c Color) is_warm() bool {
return c == .red
}