@@ -1285,19 +1285,22 @@ An _implementation_ is an item that implements a [trait](#traits) for a specific
1285
1285
Implementations are defined with the keyword ` impl ` .
1286
1286
1287
1287
~~~~
1288
- # type Point = {x: float, y: float};
1288
+ # struct Point {x: float, y: float};
1289
1289
# type Surface = int;
1290
- # type BoundingBox = {x: float, y: float, width: float, height: float};
1290
+ # struct BoundingBox {x: float, y: float, width: float, height: float};
1291
1291
# trait Shape { fn draw(Surface); fn bounding_box() -> BoundingBox; }
1292
1292
# fn do_draw_circle(s: Surface, c: Circle) { }
1293
1293
1294
- type Circle = {radius: float, center: Point};
1294
+ struct Circle {
1295
+ radius: float,
1296
+ center: Point,
1297
+ }
1295
1298
1296
1299
impl Shape for Circle {
1297
1300
fn draw(s: Surface) { do_draw_circle(s, self); }
1298
1301
fn bounding_box() -> BoundingBox {
1299
1302
let r = self.radius;
1300
- {x: self.center.x - r, y: self.center.y - r,
1303
+ BoundingBox {x: self.center.x - r, y: self.center.y - r,
1301
1304
width: 2.0 * r, height: 2.0 * r}
1302
1305
}
1303
1306
}
@@ -1637,38 +1640,6 @@ rec_expr : '{' ident ':' expr
1637
1640
[ ".." expr ] '}'
1638
1641
~~~~~~~~
1639
1642
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
-
1672
1643
### Method-call expressions
1673
1644
1674
1645
~~~~~~~~ {.ebnf .gram}
@@ -1689,7 +1660,7 @@ field_expr : expr '.' ident
1689
1660
1690
1661
A _ field expression_ consists of an expression followed by a single dot and an identifier,
1691
1662
when not immediately followed by a parenthesized expression-list (the latter is a [ method call expression] ( #method-call-expressions ) ).
1692
- 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 ) .
1693
1664
1694
1665
~~~~~~~~ {.field}
1695
1666
myrecord.myfield;
@@ -1905,8 +1876,10 @@ An example of three different swap expressions:
1905
1876
# let mut x = &mut [0];
1906
1877
# let mut a = &mut [0];
1907
1878
# let i = 0;
1908
- # let y = {mut z: 0};
1909
- # 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};
1910
1883
1911
1884
x <-> a;
1912
1885
x[i] <-> a[i];
@@ -2328,42 +2301,6 @@ match x {
2328
2301
}
2329
2302
~~~~
2330
2303
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
-
2367
2304
Patterns that bind variables default to binding to a copy of the matched value. This can be made
2368
2305
explicit using the ``` copy ``` keyword, changed to bind to a borrowed pointer by using the ``` ref ```
2369
2306
keyword, or to a mutable borrowed pointer using ``` ref mut ``` , or the value can be moved into
@@ -2692,25 +2629,6 @@ let a: List<int> = Cons(7, @Cons(13, @Nil));
2692
2629
~~~~
2693
2630
2694
2631
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
-
2714
2632
### Pointer types
2715
2633
2716
2634
All pointers in Rust are explicit first-class values.
@@ -3040,7 +2958,8 @@ Some operations (such as field selection) implicitly dereference boxes. An
3040
2958
example of an _ implicit dereference_ operation performed on box values:
3041
2959
3042
2960
~~~~~~~~
3043
- let x = @{y: 10};
2961
+ struct Foo { y: int }
2962
+ let x = @Foo{y: 10};
3044
2963
assert x.y == 10;
3045
2964
~~~~~~~~
3046
2965
0 commit comments