@@ -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,19 +1285,22 @@ 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
- # type Point = {x: float, y: float};
1288
+ # struct Point {x: float, y: float};
1303
1289
# type Surface = int;
1304
- # type BoundingBox = {x: float, y: float, width: float, height: float};
1290
+ # struct 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
- type Circle = {radius: float, center: Point};
1294
+ struct Circle {
1295
+ radius: float,
1296
+ center: Point,
1297
+ }
1309
1298
1310
1299
impl Shape for Circle {
1311
1300
fn draw(s: Surface) { do_draw_circle(s, self); }
1312
1301
fn bounding_box() -> BoundingBox {
1313
1302
let r = self.radius;
1314
- {x: self.center.x - r, y: self.center.y - r,
1303
+ BoundingBox {x: self.center.x - r, y: self.center.y - r,
1315
1304
width: 2.0 * r, height: 2.0 * r}
1316
1305
}
1317
1306
}
@@ -1604,8 +1593,7 @@ struct_expr : expr_path '{' ident ':' expr
1604
1593
[ ',' ident ':' expr ] *
1605
1594
[ ".." expr ] '}' |
1606
1595
expr_path '(' expr
1607
- [ ',' expr ] * ')' |
1608
- expr_path
1596
+ [ ',' expr ] * ')'
1609
1597
~~~~~~~~
1610
1598
1611
1599
There are several forms of structure expressions.
@@ -1615,28 +1603,23 @@ providing the field values of a new instance of the structure.
1615
1603
A field name can be any identifier, and is separated from its value expression by a colon.
1616
1604
To indicate that a field is mutable, the ` mut ` keyword is written before its name.
1617
1605
1618
- A _ tuple structure expression_ consists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1606
+ A _ tuple structure expression_ constists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1619
1607
followed by a parenthesized list of one or more comma-separated expressions
1620
1608
(in other words, the path of a structured item followed by a tuple expression).
1621
1609
The structure item must be a tuple structure item.
1622
1610
1623
- A _ unit-like structure expression_ consists only of the [ path] ( #paths ) of a [ structure item] ( #structures ) .
1624
-
1625
1611
The following are examples of structure expressions:
1626
1612
1627
1613
~~~~
1628
1614
# struct Point { x: float, y: float }
1629
1615
# struct TuplePoint(float, float);
1630
1616
# mod game { pub struct User { name: &str, age: uint, score: uint } }
1631
- # struct Cookie; fn some_fn<T>(t: T) {}
1632
1617
Point {x: 10f, y: 20f};
1633
1618
TuplePoint(10f, 20f);
1634
1619
let u = game::User {name: "Joe", age: 35u, score: 100_000};
1635
- some_fn::<Cookie>(Cookie);
1636
1620
~~~~
1637
1621
1638
1622
A structure expression forms a new value of the named structure type.
1639
- Note that for a given * unit-like* structure type, this will always be the same value.
1640
1623
1641
1624
A structure expression can terminate with the syntax ` .. ` followed by an expression to denote a functional update.
1642
1625
The expression following ` .. ` (the base) must be of the same structure type as the new structure type being formed.
@@ -1657,38 +1640,6 @@ rec_expr : '{' ident ':' expr
1657
1640
[ ".." expr ] '}'
1658
1641
~~~~~~~~
1659
1642
1660
- > ** Note:** In future versions of Rust, record expressions and [ record types] ( #record-types ) will be removed.
1661
-
1662
- A [ _ record_ ] ( #record-types ) _ expression_ is one or more comma-separated
1663
- name-value pairs enclosed by braces. A fieldname can be any identifier,
1664
- and is separated from its value expression by a
1665
- colon. To indicate that a field is mutable, the ` mut ` keyword is
1666
- written before its name.
1667
-
1668
- ~~~~
1669
- {x: 10f, y: 20f};
1670
- {name: "Joe", age: 35u, score: 100_000};
1671
- {ident: "X", mut count: 0u};
1672
- ~~~~
1673
-
1674
- The order of the fields in a record expression is significant, and
1675
- determines the type of the resulting value. ` {a: u8, b: u8} ` and `{b:
1676
- u8, a: u8}` are two different fields.
1677
-
1678
- A record expression can terminate with the syntax ` .. ` followed by an
1679
- expression to denote a functional update. The expression following
1680
- ` .. ` (the base) must be of a record type that includes at least all the
1681
- fields mentioned in the record expression. A new record will be
1682
- created, of the same type as the base expression, with the given
1683
- values for the fields that were explicitly specified, and the values
1684
- in the base record for all other fields. The ordering of the fields in
1685
- such a record expression is not significant.
1686
-
1687
- ~~~~
1688
- let base = {x: 1, y: 2, z: 3};
1689
- {y: 0, z: 10, .. base};
1690
- ~~~~
1691
-
1692
1643
### Method-call expressions
1693
1644
1694
1645
~~~~~~~~ {.ebnf .gram}
@@ -1709,7 +1660,7 @@ field_expr : expr '.' ident
1709
1660
1710
1661
A _ field expression_ consists of an expression followed by a single dot and an identifier,
1711
1662
when not immediately followed by a parenthesized expression-list (the latter is a [ method call expression] ( #method-call-expressions ) ).
1712
- A field expression denotes a field of a [ structure] ( #structure-types ) or [ record ] ( #record-types ) .
1663
+ A field expression denotes a field of a [ structure] ( #structure-types ) .
1713
1664
1714
1665
~~~~~~~~ {.field}
1715
1666
myrecord.myfield;
@@ -1925,8 +1876,10 @@ An example of three different swap expressions:
1925
1876
# let mut x = &mut [0];
1926
1877
# let mut a = &mut [0];
1927
1878
# let i = 0;
1928
- # let y = {mut z: 0};
1929
- # let b = {mut c: 0};
1879
+ # struct S1 { z: int };
1880
+ # struct S2 { c: int };
1881
+ # let mut y = S1{z: 0};
1882
+ # let mut b = S2{c: 0};
1930
1883
1931
1884
x <-> a;
1932
1885
x[i] <-> a[i];
@@ -2060,14 +2013,12 @@ an optional reference slot to serve as the function's output, bound to the
2060
2013
` lval ` on the right hand side of the call. If the function eventually returns,
2061
2014
then the expression completes.
2062
2015
2063
- Some examples of call expressions :
2016
+ An example of a call expression :
2064
2017
2065
2018
~~~~
2066
2019
# fn add(x: int, y: int) -> int { 0 }
2067
- # use core::from_str::FromStr::from_str;
2068
2020
2069
2021
let x: int = add(1, 2);
2070
- let pi = from_str::<f32>("3.14");
2071
2022
~~~~
2072
2023
2073
2024
### Lambda expressions
@@ -2350,42 +2301,6 @@ match x {
2350
2301
}
2351
2302
~~~~
2352
2303
2353
- Records and structures can also be pattern-matched and their fields bound to variables.
2354
- When matching fields of a record,
2355
- the fields being matched are specified first,
2356
- then a placeholder (` _ ` ) represents the remaining fields.
2357
-
2358
- ~~~~
2359
- # type options = {choose: bool, size: ~str};
2360
- # type player = {player: ~str, stats: (), options: options};
2361
- # fn load_stats() { }
2362
- # fn choose_player(r: &player) { }
2363
- # fn next_player() { }
2364
-
2365
- fn main() {
2366
- let r = {
2367
- player: ~"ralph",
2368
- stats: load_stats(),
2369
- options: {
2370
- choose: true,
2371
- size: ~"small"
2372
- }
2373
- };
2374
-
2375
- match r {
2376
- {options: {choose: true, _}, _} => {
2377
- choose_player(&r)
2378
- }
2379
- {player: ref p, options: {size: ~"small", _}, _} => {
2380
- log(info, (copy *p) + ~" is small");
2381
- }
2382
- _ => {
2383
- next_player();
2384
- }
2385
- }
2386
- }
2387
- ~~~~
2388
-
2389
2304
Patterns that bind variables default to binding to a copy of the matched value. This can be made
2390
2305
explicit using the ``` copy ``` keyword, changed to bind to a borrowed pointer by using the ``` ref ```
2391
2306
keyword, or to a mutable borrowed pointer using ``` ref mut ``` , or the value can be moved into
@@ -2665,10 +2580,7 @@ the resulting `struct` value will always be laid out in memory in the order spec
2665
2580
The fields of a ` struct ` may be qualified by [ visibility modifiers] ( #visibility-modifiers ) ,
2666
2581
to restrict access to implementation-private data in a structure.
2667
2582
2668
- A _ tuple struct_ type is just like a structure type, except that the fields are anonymous.
2669
-
2670
- A _ unit-like struct_ type is like a structure type, except that it has no fields.
2671
- The one value constructed by the associated [ structure expression] ( #structure-expression ) is the only value that inhabits such a type.
2583
+ A ` tuple struct ` type is just like a structure type, except that the fields are anonymous.
2672
2584
2673
2585
### Enumerated types
2674
2586
@@ -2717,25 +2629,6 @@ let a: List<int> = Cons(7, @Cons(13, @Nil));
2717
2629
~~~~
2718
2630
2719
2631
2720
- ### Record types
2721
-
2722
- > ** Note:** Records are not nominal types, thus do not directly support recursion, visibility control,
2723
- > out-of-order field initialization, or coherent trait implementation.
2724
- > Records are therefore deprecated and will be removed in future versions of Rust.
2725
- > [ Structure types] ( #structure-types ) should be used instead.
2726
-
2727
- The record type-constructor forms a new heterogeneous product of values.
2728
- Fields of a record type are accessed by name and are arranged in memory in the order specified by the record type.
2729
-
2730
- An example of a record type and its use:
2731
-
2732
- ~~~~
2733
- type Point = {x: int, y: int};
2734
- let p: Point = {x: 10, y: 11};
2735
- let px: int = p.x;
2736
- ~~~~
2737
-
2738
-
2739
2632
### Pointer types
2740
2633
2741
2634
All pointers in Rust are explicit first-class values.
@@ -3065,7 +2958,8 @@ Some operations (such as field selection) implicitly dereference boxes. An
3065
2958
example of an _ implicit dereference_ operation performed on box values:
3066
2959
3067
2960
~~~~~~~~
3068
- let x = @{y: 10};
2961
+ struct Foo { y: int }
2962
+ let x = @Foo{y: 10};
3069
2963
assert x.y == 10;
3070
2964
~~~~~~~~
3071
2965
0 commit comments