From 68baac36b438fc3ef1ec59fcdcff60e8304f140d Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 12 Jan 2015 16:07:44 -0500 Subject: [PATCH] Add enum discriminats to the reference. Fixes #15755 --- src/doc/reference.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/doc/reference.md b/src/doc/reference.md index 623097b2fc90f..59407a82e671e 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -1413,6 +1413,27 @@ a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 }; In this example, `Cat` is a _struct-like enum variant_, whereas `Dog` is simply called an enum variant. +Enums have a discriminant. You can assign them explicitly: + +``` +enum Foo { + Bar = 123, +} +``` + +If a discriminant isn't assigned, they start at zero, and add one for each +variant, in order. + +You can cast an enum to get this value: + +``` +# enum Foo { Bar = 123 } +let x = Foo::Bar as u32; // x is now 123u32 +``` + +This only works as long as none of the variants have data attached. If +it were `Bar(i32)`, this is disallowed. + ### Constant items ```{.ebnf .gram}