@@ -908,11 +908,6 @@ 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
-
916
911
Since a parameter type is opaque to the generic function, the set of
917
912
operations that can be performed on it is limited. Values of parameter
918
913
type can always be moved, but they can only be copied when the
@@ -1090,15 +1085,6 @@ let p = Point(10, 11);
1090
1085
let px: int = match p { Point(x, _) => x };
1091
1086
~~~~
1092
1087
1093
- A _ unit-like struct_ is a structure without any fields, defined by leaving off the fields list entirely.
1094
- Such types will have a single value, just like the [ unit value ` () ` ] ( #unit-and-boolean-literals ) of the unit type.
1095
- For example:
1096
-
1097
- ~~~~
1098
- struct Cookie;
1099
- let c = [Cookie, Cookie, Cookie, Cookie];
1100
- ~~~~
1101
-
1102
1088
### Enumerations
1103
1089
1104
1090
An _ enumeration_ is a simultaneous definition of a nominal [ enumerated type] ( #enumerated-types ) as well as a set of * constructors* ,
@@ -1299,22 +1285,19 @@ An _implementation_ is an item that implements a [trait](#traits) for a specific
1299
1285
Implementations are defined with the keyword ` impl ` .
1300
1286
1301
1287
~~~~
1302
- # struct Point {x: float, y: float};
1288
+ # type Point = {x: float, y: float};
1303
1289
# type Surface = int;
1304
- # struct BoundingBox {x: float, y: float, width: float, height: float};
1290
+ # type BoundingBox = {x: float, y: float, width: float, height: float};
1305
1291
# trait Shape { fn draw(Surface); fn bounding_box() -> BoundingBox; }
1306
1292
# fn do_draw_circle(s: Surface, c: Circle) { }
1307
1293
1308
- struct Circle {
1309
- radius: float,
1310
- center: Point,
1311
- }
1294
+ type Circle = {radius: float, center: Point};
1312
1295
1313
1296
impl Shape for Circle {
1314
1297
fn draw(s: Surface) { do_draw_circle(s, self); }
1315
1298
fn bounding_box() -> BoundingBox {
1316
1299
let r = self.radius;
1317
- BoundingBox {x: self.center.x - r, y: self.center.y - r,
1300
+ {x: self.center.x - r, y: self.center.y - r,
1318
1301
width: 2.0 * r, height: 2.0 * r}
1319
1302
}
1320
1303
}
@@ -1607,8 +1590,7 @@ struct_expr : expr_path '{' ident ':' expr
1607
1590
[ ',' ident ':' expr ] *
1608
1591
[ ".." expr ] '}' |
1609
1592
expr_path '(' expr
1610
- [ ',' expr ] * ')' |
1611
- expr_path
1593
+ [ ',' expr ] * ')'
1612
1594
~~~~~~~~
1613
1595
1614
1596
There are several forms of structure expressions.
@@ -1618,28 +1600,23 @@ providing the field values of a new instance of the structure.
1618
1600
A field name can be any identifier, and is separated from its value expression by a colon.
1619
1601
To indicate that a field is mutable, the ` mut ` keyword is written before its name.
1620
1602
1621
- A _ tuple structure expression_ consists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1603
+ A _ tuple structure expression_ constists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1622
1604
followed by a parenthesized list of one or more comma-separated expressions
1623
1605
(in other words, the path of a structured item followed by a tuple expression).
1624
1606
The structure item must be a tuple structure item.
1625
1607
1626
- A _ unit-like structure expression_ consists only of the [ path] ( #paths ) of a [ structure item] ( #structures ) .
1627
-
1628
1608
The following are examples of structure expressions:
1629
1609
1630
1610
~~~~
1631
1611
# struct Point { x: float, y: float }
1632
1612
# struct TuplePoint(float, float);
1633
1613
# mod game { pub struct User { name: &str, age: uint, score: uint } }
1634
- # struct Cookie; fn some_fn<T>(t: T) {}
1635
1614
Point {x: 10f, y: 20f};
1636
1615
TuplePoint(10f, 20f);
1637
1616
let u = game::User {name: "Joe", age: 35u, score: 100_000};
1638
- some_fn::<Cookie>(Cookie);
1639
1617
~~~~
1640
1618
1641
1619
A structure expression forms a new value of the named structure type.
1642
- Note that for a given * unit-like* structure type, this will always be the same value.
1643
1620
1644
1621
A structure expression can terminate with the syntax ` .. ` followed by an expression to denote a functional update.
1645
1622
The expression following ` .. ` (the base) must be of the same structure type as the new structure type being formed.
@@ -1660,6 +1637,38 @@ rec_expr : '{' ident ':' expr
1660
1637
[ ".." expr ] '}'
1661
1638
~~~~~~~~
1662
1639
1640
+ > ** Note:** In future versions of Rust, record expressions and [ record types] ( #record-types ) will be removed.
1641
+
1642
+ A [ _ record_ ] ( #record-types ) _ expression_ is one or more comma-separated
1643
+ name-value pairs enclosed by braces. A fieldname can be any identifier,
1644
+ and is separated from its value expression by a
1645
+ colon. To indicate that a field is mutable, the ` mut ` keyword is
1646
+ written before its name.
1647
+
1648
+ ~~~~
1649
+ {x: 10f, y: 20f};
1650
+ {name: "Joe", age: 35u, score: 100_000};
1651
+ {ident: "X", mut count: 0u};
1652
+ ~~~~
1653
+
1654
+ The order of the fields in a record expression is significant, and
1655
+ determines the type of the resulting value. ` {a: u8, b: u8} ` and `{b:
1656
+ u8, a: u8}` are two different fields.
1657
+
1658
+ A record expression can terminate with the syntax ` .. ` followed by an
1659
+ expression to denote a functional update. The expression following
1660
+ ` .. ` (the base) must be of a record type that includes at least all the
1661
+ fields mentioned in the record expression. A new record will be
1662
+ created, of the same type as the base expression, with the given
1663
+ values for the fields that were explicitly specified, and the values
1664
+ in the base record for all other fields. The ordering of the fields in
1665
+ such a record expression is not significant.
1666
+
1667
+ ~~~~
1668
+ let base = {x: 1, y: 2, z: 3};
1669
+ {y: 0, z: 10, .. base};
1670
+ ~~~~
1671
+
1663
1672
### Method-call expressions
1664
1673
1665
1674
~~~~~~~~ {.ebnf .gram}
@@ -1680,7 +1689,7 @@ field_expr : expr '.' ident
1680
1689
1681
1690
A _ field expression_ consists of an expression followed by a single dot and an identifier,
1682
1691
when not immediately followed by a parenthesized expression-list (the latter is a [ method call expression] ( #method-call-expressions ) ).
1683
- A field expression denotes a field of a [ structure] ( #structure-types ) .
1692
+ A field expression denotes a field of a [ structure] ( #structure-types ) or [ record ] ( #record-types ) .
1684
1693
1685
1694
~~~~~~~~ {.field}
1686
1695
myrecord.myfield;
@@ -1896,10 +1905,8 @@ An example of three different swap expressions:
1896
1905
# let mut x = &mut [0];
1897
1906
# let mut a = &mut [0];
1898
1907
# let i = 0;
1899
- # struct S1 { z: int };
1900
- # struct S2 { c: int };
1901
- # let mut y = S1{z: 0};
1902
- # let mut b = S2{c: 0};
1908
+ # let y = {mut z: 0};
1909
+ # let b = {mut c: 0};
1903
1910
1904
1911
x <-> a;
1905
1912
x[i] <-> a[i];
@@ -2033,14 +2040,12 @@ an optional reference slot to serve as the function's output, bound to the
2033
2040
` lval ` on the right hand side of the call. If the function eventually returns,
2034
2041
then the expression completes.
2035
2042
2036
- Some examples of call expressions :
2043
+ An example of a call expression :
2037
2044
2038
2045
~~~~
2039
2046
# fn add(x: int, y: int) -> int { 0 }
2040
- # use core::from_str::FromStr::from_str;
2041
2047
2042
2048
let x: int = add(1, 2);
2043
- let pi = from_str::<f32>("3.14");
2044
2049
~~~~
2045
2050
2046
2051
### Lambda expressions
@@ -2323,6 +2328,42 @@ match x {
2323
2328
}
2324
2329
~~~~
2325
2330
2331
+ Records and structures can also be pattern-matched and their fields bound to variables.
2332
+ When matching fields of a record,
2333
+ the fields being matched are specified first,
2334
+ then a placeholder (` _ ` ) represents the remaining fields.
2335
+
2336
+ ~~~~
2337
+ # type options = {choose: bool, size: ~str};
2338
+ # type player = {player: ~str, stats: (), options: options};
2339
+ # fn load_stats() { }
2340
+ # fn choose_player(r: &player) { }
2341
+ # fn next_player() { }
2342
+
2343
+ fn main() {
2344
+ let r = {
2345
+ player: ~"ralph",
2346
+ stats: load_stats(),
2347
+ options: {
2348
+ choose: true,
2349
+ size: ~"small"
2350
+ }
2351
+ };
2352
+
2353
+ match r {
2354
+ {options: {choose: true, _}, _} => {
2355
+ choose_player(&r)
2356
+ }
2357
+ {player: ref p, options: {size: ~"small", _}, _} => {
2358
+ log(info, (copy *p) + ~" is small");
2359
+ }
2360
+ _ => {
2361
+ next_player();
2362
+ }
2363
+ }
2364
+ }
2365
+ ~~~~
2366
+
2326
2367
Patterns that bind variables default to binding to a copy of the matched value. This can be made
2327
2368
explicit using the ``` copy ``` keyword, changed to bind to a borrowed pointer by using the ``` ref ```
2328
2369
keyword, or to a mutable borrowed pointer using ``` ref mut ``` , or the value can be moved into
@@ -2602,10 +2643,7 @@ the resulting `struct` value will always be laid out in memory in the order spec
2602
2643
The fields of a ` struct ` may be qualified by [ visibility modifiers] ( #visibility-modifiers ) ,
2603
2644
to restrict access to implementation-private data in a structure.
2604
2645
2605
- A _ tuple struct_ type is just like a structure type, except that the fields are anonymous.
2606
-
2607
- A _ unit-like struct_ type is like a structure type, except that it has no fields.
2608
- The one value constructed by the associated [ structure expression] ( #structure-expression ) is the only value that inhabits such a type.
2646
+ A ` tuple struct ` type is just like a structure type, except that the fields are anonymous.
2609
2647
2610
2648
### Enumerated types
2611
2649
@@ -2654,6 +2692,25 @@ let a: List<int> = Cons(7, @Cons(13, @Nil));
2654
2692
~~~~
2655
2693
2656
2694
2695
+ ### Record types
2696
+
2697
+ > ** Note:** Records are not nominal types, thus do not directly support recursion, visibility control,
2698
+ > out-of-order field initialization, or coherent trait implementation.
2699
+ > Records are therefore deprecated and will be removed in future versions of Rust.
2700
+ > [ Structure types] ( #structure-types ) should be used instead.
2701
+
2702
+ The record type-constructor forms a new heterogeneous product of values.
2703
+ Fields of a record type are accessed by name and are arranged in memory in the order specified by the record type.
2704
+
2705
+ An example of a record type and its use:
2706
+
2707
+ ~~~~
2708
+ type Point = {x: int, y: int};
2709
+ let p: Point = {x: 10, y: 11};
2710
+ let px: int = p.x;
2711
+ ~~~~
2712
+
2713
+
2657
2714
### Pointer types
2658
2715
2659
2716
All pointers in Rust are explicit first-class values.
@@ -2983,8 +3040,7 @@ Some operations (such as field selection) implicitly dereference boxes. An
2983
3040
example of an _ implicit dereference_ operation performed on box values:
2984
3041
2985
3042
~~~~~~~~
2986
- struct Foo { y: int }
2987
- let x = @Foo{y: 10};
3043
+ let x = @{y: 10};
2988
3044
assert x.y == 10;
2989
3045
~~~~~~~~
2990
3046
0 commit comments