Skip to content

Commit a611e04

Browse files
committed
---
yaml --- r: 51267 b: refs/heads/incoming c: 1fcb044 h: refs/heads/master i: 51265: 9ac606c 51263: e45833c v: v3
1 parent a00bcaf commit a611e04

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: 8eb2bab100b42f0ba751552d8eff00eb2134c55a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/incoming: 7538450b8d5e831dca7891bdd54ebdf25d865970
9+
refs/heads/incoming: 1fcb0443cf2a5575691b70e7ef80e9720bc4bc07
1010
refs/heads/dist-snap: 8b98e5a296d95c5e832db0756828e5bec31c6f50
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/doc/tutorial.md

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -681,45 +681,6 @@ the value of `North` is 0, `East` is 1, `South` is 2, and `West` is 3.
681681
When an enum is C-like, you can apply the `as` cast operator to
682682
convert it to its discriminator value as an `int`.
683683

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-
723684
For enum types with multiple variants, destructuring is the only way to
724685
get at their contents. All variant constructors can be used as
725686
patterns, as in this definition of `area`:
@@ -789,10 +750,10 @@ match mytup {
789750

790751
## Tuple structs
791752

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.
796757

797758
For example:
798759
~~~~
@@ -803,6 +764,37 @@ match mytup {
803764
}
804765
~~~~
805766

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+
806798
# Functions
807799

808800
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.
22942286
pub mod farm {
22952287
# pub type Chicken = int;
22962288
# type Cow = int;
2297-
# enum Human = int;
2289+
# struct Human(int);
22982290
# impl Human { fn rest(&self) { } }
22992291
# pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], cows: ~[], farmer: Human(0) } }
23002292
pub struct Farm {

0 commit comments

Comments
 (0)