@@ -647,31 +647,8 @@ match mypoint {
647
647
648
648
## Enums
649
649
650
- Enums are datatypes that have several alternate representations. For
651
- example, consider the following type:
652
-
653
- ~~~~
654
- # struct Point { x: f64, y: f64 }
655
- enum Shape {
656
- Circle(Point, f64),
657
- Rectangle(Point, Point)
658
- }
659
- ~~~~
660
-
661
- A value of this type is either a ` Circle ` , in which case it contains a
662
- ` Point ` struct and a f64, or a ` Rectangle ` , in which case it contains
663
- two ` Point ` structs. The run-time representation of such a value
664
- includes an identifier of the actual form that it holds, much like the
665
- "tagged union" pattern in C, but with better static guarantees.
666
-
667
- The above declaration will define a type ` Shape ` that can refer to
668
- such shapes, and two functions, ` Circle ` and ` Rectangle ` , which can be
669
- used to construct values of the type (taking arguments of the
670
- specified types). So ` Circle(Point { x: 0.0, y: 0.0 }, 10.0) ` is the way to
671
- create a new circle.
672
-
673
- Enum variants need not have parameters. This ` enum ` declaration,
674
- for example, is equivalent to a C enum:
650
+ Enums are datatypes with several alternate representations. A simple ` enum `
651
+ defines one or more constants, all of which have the same type:
675
652
676
653
~~~~
677
654
enum Direction {
@@ -682,12 +659,21 @@ enum Direction {
682
659
}
683
660
~~~~
684
661
685
- This declaration defines ` North ` , ` East ` , ` South ` , and ` West ` as constants,
686
- all of which have type ` Direction ` .
662
+ Each variant of this enum has a unique and constant integral discriminator
663
+ value. If no explicit discriminator is specified for a variant, the value
664
+ defaults to the value of the previous variant plus one. If the first variant
665
+ does not have a discriminator, it defaults to 0. For example, the value of
666
+ ` North ` is 0, ` East ` is 1, ` South ` is 2, and ` West ` is 3.
687
667
688
- When an enum is C-like (that is, when none of the variants have
689
- parameters), it is possible to explicitly set the discriminator values
690
- to a constant value:
668
+ When an enum has simple integer discriminators, you can apply the ` as ` cast
669
+ operator to convert a variant to its discriminator value as an ` int ` :
670
+
671
+ ~~~~
672
+ # enum Direction { North }
673
+ println!( "{:?} => {}", North, North as int );
674
+ ~~~~
675
+
676
+ It is possible to set the discriminator values to chosen constant values:
691
677
692
678
~~~~
693
679
enum Color {
@@ -697,17 +683,30 @@ enum Color {
697
683
}
698
684
~~~~
699
685
700
- If an explicit discriminator is not specified for a variant, the value
701
- defaults to the value of the previous variant plus one. If the first
702
- variant does not have a discriminator, it defaults to 0. For example,
703
- the value of ` North ` is 0, ` East ` is 1, ` South ` is 2, and ` West ` is 3.
686
+ Variants do not have to be simple values; they may be more complex:
704
687
705
- When an enum is C-like, you can apply the ` as ` cast operator to
706
- convert it to its discriminator value as an ` int ` .
688
+ ~~~~
689
+ # struct Point { x: f64, y: f64 }
690
+ enum Shape {
691
+ Circle(Point, f64),
692
+ Rectangle(Point, Point)
693
+ }
694
+ ~~~~
707
695
708
- For enum types with multiple variants, destructuring is the only way to
709
- get at their contents. All variant constructors can be used as
710
- patterns, as in this definition of ` area ` :
696
+ A value of this type is either a ` Circle ` , in which case it contains a
697
+ ` Point ` struct and a f64, or a ` Rectangle ` , in which case it contains
698
+ two ` Point ` structs. The run-time representation of such a value
699
+ includes an identifier of the actual form that it holds, much like the
700
+ "tagged union" pattern in C, but with better static guarantees.
701
+
702
+ This declaration defines a type ` Shape ` that can refer to such shapes, and two
703
+ functions, ` Circle ` and ` Rectangle ` , which can be used to construct values of
704
+ the type. To create a new Circle, write `Circle(Point { x: 0.0, y: 0.0 },
705
+ 10.0)`.
706
+
707
+ All of these variant constructors may be used as patterns. The only way to
708
+ access the contents of an enum instance is the destructuring of a match. For
709
+ example:
711
710
712
711
~~~~
713
712
use std::f64;
@@ -721,10 +720,8 @@ fn area(sh: Shape) -> f64 {
721
720
}
722
721
~~~~
723
722
724
- You can write a lone ` _ ` to ignore an individual field, and can
725
- ignore all fields of a variant like: ` Circle(..) ` . As in their
726
- introduction form, nullary enum patterns are written without
727
- parentheses.
723
+ Use a lone ` _ ` to ignore an individual field. Ignore all fields of a variant
724
+ like: ` Circle(..) ` . Nullary enum patterns are written without parentheses:
728
725
729
726
~~~~
730
727
# struct Point { x: f64, y: f64 }
0 commit comments