doc: Remove documentation on newtype enums.
This commit is contained in:
parent
7538450b8d
commit
1fcb0443cf
@ -681,45 +681,6 @@ the value of `North` is 0, `East` is 1, `South` is 2, and `West` is 3.
|
||||
When an enum is C-like, you can apply the `as` cast operator to
|
||||
convert it to its discriminator value as an `int`.
|
||||
|
||||
<a name="single_variant_enum"></a>
|
||||
|
||||
There is a special case for enums with a single variant, which are
|
||||
sometimes called "newtype-style enums" (after Haskell's "newtype"
|
||||
feature). These are used to define new types in such a way that the
|
||||
new name is not just a synonym for an existing type, but its own
|
||||
distinct type: `type` creates a structural synonym, while this form of
|
||||
`enum` creates a nominal synonym. If you say:
|
||||
|
||||
~~~~
|
||||
enum GizmoId = int;
|
||||
~~~~
|
||||
|
||||
That is a shorthand for this:
|
||||
|
||||
~~~~
|
||||
enum GizmoId { GizmoId(int) }
|
||||
~~~~
|
||||
|
||||
You can extract the contents of such an enum type with the
|
||||
dereference (`*`) unary operator:
|
||||
|
||||
~~~~
|
||||
# enum GizmoId = int;
|
||||
let my_gizmo_id: GizmoId = GizmoId(10);
|
||||
let id_int: int = *my_gizmo_id;
|
||||
~~~~
|
||||
|
||||
Types like this can be useful to differentiate between data that have
|
||||
the same type but must be used in different ways.
|
||||
|
||||
~~~~
|
||||
enum Inches = int;
|
||||
enum Centimeters = int;
|
||||
~~~~
|
||||
|
||||
The above definitions allow for a simple way for programs to avoid
|
||||
confusing numbers that correspond to different units.
|
||||
|
||||
For enum types with multiple variants, destructuring is the only way to
|
||||
get at their contents. All variant constructors can be used as
|
||||
patterns, as in this definition of `area`:
|
||||
@ -789,10 +750,10 @@ match mytup {
|
||||
|
||||
## Tuple structs
|
||||
|
||||
Rust also has _nominal tuples_, which behave like both structs and tuples,
|
||||
except that nominal tuple types have names
|
||||
(so `Foo(1, 2)` has a different type from `Bar(1, 2)`),
|
||||
and nominal tuple types' _fields_ do not have names.
|
||||
Rust also has _tuple structs_, which behave like both structs and tuples,
|
||||
except that, unlike tuples, tuple structs have names (so `Foo(1, 2)` has a
|
||||
different type from `Bar(1, 2)`), and tuple structs' _fields_ do not have
|
||||
names.
|
||||
|
||||
For example:
|
||||
~~~~
|
||||
@ -803,6 +764,37 @@ match mytup {
|
||||
}
|
||||
~~~~
|
||||
|
||||
<a name="newtype"></a>
|
||||
|
||||
There is a special case for tuple structs with a single field, which are
|
||||
sometimes called "newtypes" (after Haskell's "newtype" feature). These are
|
||||
used to define new types in such a way that the new name is not just a
|
||||
synonym for an existing type but is rather its own distinct type.
|
||||
|
||||
~~~~
|
||||
struct GizmoId(int);
|
||||
~~~~
|
||||
|
||||
For convenience, you can extract the contents of such a struct with the
|
||||
dereference (`*`) unary operator:
|
||||
|
||||
~~~~
|
||||
# struct GizmoId(int);
|
||||
let my_gizmo_id: GizmoId = GizmoId(10);
|
||||
let id_int: int = *my_gizmo_id;
|
||||
~~~~
|
||||
|
||||
Types like this can be useful to differentiate between data that have
|
||||
the same type but must be used in different ways.
|
||||
|
||||
~~~~
|
||||
struct Inches(int);
|
||||
struct Centimeters(int);
|
||||
~~~~
|
||||
|
||||
The above definitions allow for a simple way for programs to avoid
|
||||
confusing numbers that correspond to different units.
|
||||
|
||||
# Functions
|
||||
|
||||
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.
|
||||
pub mod farm {
|
||||
# pub type Chicken = int;
|
||||
# type Cow = int;
|
||||
# enum Human = int;
|
||||
# struct Human(int);
|
||||
# impl Human { fn rest(&self) { } }
|
||||
# pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], cows: ~[], farmer: Human(0) } }
|
||||
pub struct Farm {
|
||||
|
Loading…
x
Reference in New Issue
Block a user