Skip to content

Commit ddf3a8c

Browse files
committed
---
yaml --- r: 49825 b: refs/heads/auto c: 1fcb044 h: refs/heads/master i: 49823: b32104c v: v3
1 parent 5aa0ecf commit ddf3a8c

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
@@ -14,5 +14,5 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1414
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1515
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1616
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
17-
refs/heads/auto: 7538450b8d5e831dca7891bdd54ebdf25d865970
17+
refs/heads/auto: 1fcb0443cf2a5575691b70e7ef80e9720bc4bc07
1818
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167

branches/auto/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)