Enumerations
Enums define a type with a fixed set of named variants.
Basic Enum
kode
enum Color {
Red,
Green,
Blue
}
enum Status {
Active,
Inactive,
Pending
}Enum with Associated Values
kode
enum Result {
Success(value: int),
Error(message: string),
Pending
}
let outcome = Result.Success(42)
let error = Result.Error("Connection timeout")Pattern Matching with Enums
kode
match (outcome) {
Result.Success(value) => print("Got: ${value}"),
Result.Error(msg) => print("Error: ${msg}"),
Result.Pending => print("Still waiting...")
}Enum with Methods
kode
enum Status {
Active,
Inactive,
Pending
}
impl Status {
fn isReady() -> bool {
return this == Status.Active
}
}
let status = Status.Active
if (status.isReady()) {
print("Ready to go!")
}