@@ -681,45 +681,6 @@ the value of `North` is 0, `East` is 1, `South` is 2, and `West` is 3.
681
681
When an enum is C-like, you can apply the ` as ` cast operator to
682
682
convert it to its discriminator value as an ` int ` .
683
683
684
- <a name =" single_variant_enum " ></a >
685
-
686
- There is a special case for enums with a single variant, which are
687
- sometimes called "newtype-style enums" (after Haskell's "newtype"
688
- feature). These are used to define new types in such a way that the
689
- new name is not just a synonym for an existing type, but its own
690
- distinct type: ` type ` creates a structural synonym, while this form of
691
- ` enum ` creates a nominal synonym. If you say:
692
-
693
- ~~~~
694
- enum GizmoId = int;
695
- ~~~~
696
-
697
- That is a shorthand for this:
698
-
699
- ~~~~
700
- enum GizmoId { GizmoId(int) }
701
- ~~~~
702
-
703
- You can extract the contents of such an enum type with the
704
- dereference (` * ` ) unary operator:
705
-
706
- ~~~~
707
- # enum GizmoId = int;
708
- let my_gizmo_id: GizmoId = GizmoId(10);
709
- let id_int: int = *my_gizmo_id;
710
- ~~~~
711
-
712
- Types like this can be useful to differentiate between data that have
713
- the same type but must be used in different ways.
714
-
715
- ~~~~
716
- enum Inches = int;
717
- enum Centimeters = int;
718
- ~~~~
719
-
720
- The above definitions allow for a simple way for programs to avoid
721
- confusing numbers that correspond to different units.
722
-
723
684
For enum types with multiple variants, destructuring is the only way to
724
685
get at their contents. All variant constructors can be used as
725
686
patterns, as in this definition of ` area ` :
@@ -789,10 +750,10 @@ match mytup {
789
750
790
751
## Tuple structs
791
752
792
- Rust also has _ nominal tuples _ , which behave like both structs and tuples,
793
- except that nominal tuple types have names
794
- (so ` Foo(1, 2) ` has a different type from ` Bar(1, 2) ` ),
795
- and nominal tuple types' _ fields _ do not have names.
753
+ Rust also has _ tuple structs _ , which behave like both structs and tuples,
754
+ except that, unlike tuples, tuple structs have names (so ` Foo(1, 2) ` has a
755
+ different type from ` Bar(1, 2) ` ), and tuple structs' _ fields _ do not have
756
+ names.
796
757
797
758
For example:
798
759
~~~~
@@ -803,6 +764,37 @@ match mytup {
803
764
}
804
765
~~~~
805
766
767
+ <a name =" newtype " ></a >
768
+
769
+ There is a special case for tuple structs with a single field, which are
770
+ sometimes called "newtypes" (after Haskell's "newtype" feature). These are
771
+ used to define new types in such a way that the new name is not just a
772
+ synonym for an existing type but is rather its own distinct type.
773
+
774
+ ~~~~
775
+ struct GizmoId(int);
776
+ ~~~~
777
+
778
+ For convenience, you can extract the contents of such a struct with the
779
+ dereference (` * ` ) unary operator:
780
+
781
+ ~~~~
782
+ # struct GizmoId(int);
783
+ let my_gizmo_id: GizmoId = GizmoId(10);
784
+ let id_int: int = *my_gizmo_id;
785
+ ~~~~
786
+
787
+ Types like this can be useful to differentiate between data that have
788
+ the same type but must be used in different ways.
789
+
790
+ ~~~~
791
+ struct Inches(int);
792
+ struct Centimeters(int);
793
+ ~~~~
794
+
795
+ The above definitions allow for a simple way for programs to avoid
796
+ confusing numbers that correspond to different units.
797
+
806
798
# Functions
807
799
808
800
We've already seen several function definitions. Like all other static
@@ -2294,7 +2286,7 @@ struct level. Note that fields and methods are _public_ by default.
2294
2286
pub mod farm {
2295
2287
# pub type Chicken = int;
2296
2288
# type Cow = int;
2297
- # enum Human = int;
2289
+ # struct Human( int) ;
2298
2290
# impl Human { fn rest(&self) { } }
2299
2291
# pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], cows: ~[], farmer: Human(0) } }
2300
2292
pub struct Farm {
0 commit comments