enum
Source: compiler/src/builtin_docs/keywords.md Declares a sum type: a value is exactly one of its variants.
Variants can carry no data, a tuple payload or named fields. Build them with
Enum::Variant and take them apart with match. With #[repr(int)] (or
string, float, bool), each variant is backed by a literal (Ok = 200).
An enum may be generic.
enum Shape {
Circle(float),
Rect { w: float, h: float },
Empty,
}
fn area(Shape s) -> float {
return match s {
Shape::Circle(r) => 3.14 * r * r,
Shape::Rect { w, h } => w * h,
Shape::Empty => 0.0,
};
}
See also#
stringfloatboolfn: Declares a function, a method, or (written in an expression) an anonymous function.let: Declares a mutable local variable.const: Declares a binding that cannot be reassigned: a local, a module constant, or a class field that is set only bynew.static: Declares a module-level or class-level slot shared by the whole program, or a method withoutself.readonly: A read-only view of a value: nothing reachable through it can be changed.