@@ -681,45 +681,6 @@ the value of `North` is 0, `East` is 1, `South` is 2, and `West` is 3.
681681When an enum is C-like, you can apply the ` as ` cast operator to
682682convert 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-
723684For enum types with multiple variants, destructuring is the only way to
724685get at their contents. All variant constructors can be used as
725686patterns, 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
797758For 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
808800We've already seen several function definitions. Like all other static
@@ -1369,7 +1361,7 @@ the enclosing scope.
13691361
13701362~~~~
13711363# use println = core::io::println;
1372- fn call_closure_with_ten(b: fn(int)) { b(10); }
1364+ fn call_closure_with_ten(b: & fn(int)) { b(10); }
13731365
13741366let captured_var = 20;
13751367let closure = |arg| println(fmt!("captured_var=%d, arg=%d", captured_var, arg));
@@ -1455,7 +1447,7 @@ should almost always declare the type of that argument as `fn()`. That way,
14551447callers may pass any kind of closure.
14561448
14571449~~~~
1458- fn call_twice(f: fn()) { f(); f(); }
1450+ fn call_twice(f: & fn()) { f(); f(); }
14591451let closure = || { "I'm a closure, and it doesn't matter what type I am"; };
14601452fn function() { "I'm a normal function"; }
14611453call_twice(closure);
@@ -1475,7 +1467,7 @@ Consider this function that iterates over a vector of
14751467integers, passing in a pointer to each integer in the vector:
14761468
14771469~~~~
1478- fn each(v: &[int], op: fn(v: &int)) {
1470+ fn each(v: &[int], op: & fn(v: &int)) {
14791471 let mut n = 0;
14801472 while n < v.len() {
14811473 op(&v[n]);
@@ -1496,7 +1488,7 @@ argument, we can write it in a way that has a pleasant, block-like
14961488structure.
14971489
14981490~~~~
1499- # fn each(v: &[int], op: fn(v: &int)) { }
1491+ # fn each(v: &[int], op: & fn(v: &int)) { }
15001492# fn do_some_work(i: &int) { }
15011493each([1, 2, 3], |n| {
15021494 do_some_work(n);
@@ -1507,7 +1499,7 @@ This is such a useful pattern that Rust has a special form of function
15071499call that can be written more like a built-in control structure:
15081500
15091501~~~~
1510- # fn each(v: &[int], op: fn(v: &int)) { }
1502+ # fn each(v: &[int], op: & fn(v: &int)) { }
15111503# fn do_some_work(i: &int) { }
15121504do each([1, 2, 3]) |n| {
15131505 do_some_work(n);
@@ -1554,7 +1546,7 @@ Consider again our `each` function, this time improved to
15541546break early when the iteratee returns ` false ` :
15551547
15561548~~~~
1557- fn each(v: &[int], op: fn(v: &int) -> bool) {
1549+ fn each(v: &[int], op: & fn(v: &int) -> bool) {
15581550 let mut n = 0;
15591551 while n < v.len() {
15601552 if !op(&v[n]) {
@@ -1778,7 +1770,7 @@ vector consisting of the result of applying `function` to each element
17781770of ` vector ` :
17791771
17801772~~~~
1781- fn map<T, U>(vector: &[T], function: fn(v: &T) -> U) -> ~[U] {
1773+ fn map<T, U>(vector: &[T], function: & fn(v: &T) -> U) -> ~[U] {
17821774 let mut accumulator = ~[];
17831775 for vec::each(vector) |element| {
17841776 accumulator.push(function(element));
@@ -1977,12 +1969,12 @@ types might look like the following:
19771969~~~~
19781970trait Seq<T> {
19791971 fn len(&self) -> uint;
1980- fn iter(&self, b: fn(v: &T));
1972+ fn iter(&self, b: & fn(v: &T));
19811973}
19821974
19831975impl<T> Seq<T> for ~[T] {
19841976 fn len(&self) -> uint { vec::len(*self) }
1985- fn iter(&self, b: fn(v: &T)) {
1977+ fn iter(&self, b: & fn(v: &T)) {
19861978 for vec::each(*self) |elt| { b(elt); }
19871979 }
19881980}
@@ -2294,7 +2286,7 @@ struct level. Note that fields and methods are _public_ by default.
22942286pub 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