@@ -908,6 +908,11 @@ function defined above on `[1, 2]` will instantiate type parameter `T`
908
908
with ` int ` , and require the closure parameter to have type
909
909
` fn(int) ` .
910
910
911
+ The type parameters can also be explicitly supplied in a trailing
912
+ [ path] ( #paths ) component after the function name. This might be necessary
913
+ if there is not sufficient context to determine the type parameters. For
914
+ example, ` sys::size_of::<u32>() == 4 ` .
915
+
911
916
Since a parameter type is opaque to the generic function, the set of
912
917
operations that can be performed on it is limited. Values of parameter
913
918
type can always be moved, but they can only be copied when the
@@ -1085,15 +1090,6 @@ let p = Point(10, 11);
1085
1090
let px: int = match p { Point(x, _) => x };
1086
1091
~~~~
1087
1092
1088
- A _ unit-like struct_ is a structure without any fields, defined by leaving off the fields list entirely.
1089
- Such types will have a single value, just like the [ unit value ` () ` ] ( #unit-and-boolean-literals ) of the unit type.
1090
- For example:
1091
-
1092
- ~~~~
1093
- struct Cookie;
1094
- let c = [Cookie, Cookie, Cookie, Cookie];
1095
- ~~~~
1096
-
1097
1093
### Enumerations
1098
1094
1099
1095
An _ enumeration_ is a simultaneous definition of a nominal [ enumerated type] ( #enumerated-types ) as well as a set of * constructors* ,
@@ -1599,8 +1595,7 @@ struct_expr : expr_path '{' ident ':' expr
1599
1595
[ ',' ident ':' expr ] *
1600
1596
[ ".." expr ] '}' |
1601
1597
expr_path '(' expr
1602
- [ ',' expr ] * ')' |
1603
- expr_path
1598
+ [ ',' expr ] * ')'
1604
1599
~~~~~~~~
1605
1600
1606
1601
There are several forms of structure expressions.
@@ -1610,28 +1605,23 @@ providing the field values of a new instance of the structure.
1610
1605
A field name can be any identifier, and is separated from its value expression by a colon.
1611
1606
To indicate that a field is mutable, the ` mut ` keyword is written before its name.
1612
1607
1613
- A _ tuple structure expression_ consists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1608
+ A _ tuple structure expression_ constists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1614
1609
followed by a parenthesized list of one or more comma-separated expressions
1615
1610
(in other words, the path of a structured item followed by a tuple expression).
1616
1611
The structure item must be a tuple structure item.
1617
1612
1618
- A _ unit-like structure expression_ consists only of the [ path] ( #paths ) of a [ structure item] ( #structures ) .
1619
-
1620
1613
The following are examples of structure expressions:
1621
1614
1622
1615
~~~~
1623
1616
# struct Point { x: float, y: float }
1624
1617
# struct TuplePoint(float, float);
1625
1618
# mod game { pub struct User { name: &str, age: uint, score: uint } }
1626
- # struct Cookie; fn some_fn<T>(t: T) {}
1627
1619
Point {x: 10f, y: 20f};
1628
1620
TuplePoint(10f, 20f);
1629
1621
let u = game::User {name: "Joe", age: 35u, score: 100_000};
1630
- some_fn::<Cookie>(Cookie);
1631
1622
~~~~
1632
1623
1633
1624
A structure expression forms a new value of the named structure type.
1634
- Note that for a given * unit-like* structure type, this will always be the same value.
1635
1625
1636
1626
A structure expression can terminate with the syntax ` .. ` followed by an expression to denote a functional update.
1637
1627
The expression following ` .. ` (the base) must be of the same structure type as the new structure type being formed.
@@ -2055,12 +2045,14 @@ an optional reference slot to serve as the function's output, bound to the
2055
2045
` lval ` on the right hand side of the call. If the function eventually returns,
2056
2046
then the expression completes.
2057
2047
2058
- An example of a call expression :
2048
+ Some examples of call expressions :
2059
2049
2060
2050
~~~~
2061
2051
# fn add(x: int, y: int) -> int { 0 }
2052
+ # use core::from_str::FromStr::from_str;
2062
2053
2063
2054
let x: int = add(1, 2);
2055
+ let pi = from_str::<f32>("3.14");
2064
2056
~~~~
2065
2057
2066
2058
### Lambda expressions
@@ -2658,10 +2650,7 @@ the resulting `struct` value will always be laid out in memory in the order spec
2658
2650
The fields of a ` struct ` may be qualified by [ visibility modifiers] ( #visibility-modifiers ) ,
2659
2651
to restrict access to implementation-private data in a structure.
2660
2652
2661
- A _ tuple struct_ type is just like a structure type, except that the fields are anonymous.
2662
-
2663
- A _ unit-like struct_ type is like a structure type, except that it has no fields.
2664
- The one value constructed by the associated [ structure expression] ( #structure-expression ) is the only value that inhabits such a type.
2653
+ A ` tuple struct ` type is just like a structure type, except that the fields are anonymous.
2665
2654
2666
2655
### Enumerated types
2667
2656
0 commit comments