@@ -705,6 +705,7 @@ There are several kinds of item:
705
705
* [ modules] ( #modules )
706
706
* [ functions] ( #functions )
707
707
* [ type definitions] ( #type-definitions )
708
+ * [ structures] ( #structures )
708
709
* [ enumerations] ( #enumerations )
709
710
* [ constants] ( #constants )
710
711
* [ traits] ( #traits )
@@ -1111,49 +1112,35 @@ that are composite records, each containing two unsigned 8-bit integers
1111
1112
accessed through the components ` x ` and ` y ` , and laid out in memory with the
1112
1113
` x ` component preceding the ` y ` component.
1113
1114
1114
- ### Enumerations
1115
-
1116
- An _ enumeration item_ simultaneously declares a new nominal
1117
- [ enumerated type] ( #enumerated-types ) as well as a set of * constructors* that
1118
- can be used to create or pattern-match values of the corresponding enumerated
1119
- type.
1115
+ ### Structures
1120
1116
1121
- The constructors of an ` enum ` type may be recursive: that is, each constructor
1122
- may take an argument that refers, directly or indirectly, to the enumerated
1123
- type the constructor is a member of. Such recursion has restrictions:
1117
+ A _ structure_ is a nominal [ structure type] ( #structure-types ) defined with the keyword ` struct ` .
1124
1118
1125
- * Recursive types can be introduced only through ` enum ` constructors.
1126
- * A recursive ` enum ` item must have at least one non-recursive constructor (in
1127
- order to give the recursion a basis case).
1128
- * The recursive argument of recursive ` enum ` constructors must be [ * box*
1129
- values] ( #box-types ) (in order to bound the in-memory size of the
1130
- constructor).
1131
- * Recursive type definitions can cross module boundaries, but not module
1132
- * visibility* boundaries or crate boundaries (in order to simplify the
1133
- module system).
1119
+ An example of a ` struct ` item and its use:
1134
1120
1121
+ ~~~~
1122
+ struct Point {x: int, y: int}
1123
+ let p = Point {x: 10, y: 11};
1124
+ let px: int = p.x;
1125
+ ~~~~
1135
1126
1136
- An example of an ` enum ` item and its use:
1127
+ ### Enumerations
1137
1128
1138
- ~~~~
1139
- enum animal {
1140
- dog,
1141
- cat
1142
- }
1129
+ An _ enumeration_ is a simulatneous definition of a nominal [ enumerated type] ( #enumerated-types ) as well as a set of * constructors* ,
1130
+ that can be used to create or pattern-match values of the corresponding enumerated type.
1143
1131
1144
- let mut a: animal = dog;
1145
- a = cat;
1146
- ~~~~
1132
+ Enumerations are declared with the keyword ` enum ` .
1147
1133
1148
- An example of a * recursive * ` enum ` item and its use:
1134
+ An example of an ` enum ` item and its use:
1149
1135
1150
1136
~~~~
1151
- enum list<T> {
1152
- nil ,
1153
- cons(T, @list<T>)
1137
+ enum Animal {
1138
+ Dog ,
1139
+ Cat
1154
1140
}
1155
1141
1156
- let a: list<int> = cons(7, @cons(13, @nil));
1142
+ let mut a: Animal = Dog;
1143
+ a = Cat;
1157
1144
~~~~
1158
1145
1159
1146
### Constants
@@ -2374,22 +2361,6 @@ A value of type `~str` is a Unicode string, represented as a vector of 8-bit
2374
2361
unsigned bytes holding a sequence of UTF-8 codepoints.
2375
2362
2376
2363
2377
- ### Record types
2378
-
2379
- The record type-constructor forms a new heterogeneous product of values.^[ The
2380
- record type-constructor is analogous to the ` struct ` type-constructor in the
2381
- Algol/C family, the * record* types of the ML family, or the * structure* types
2382
- of the Lisp family.] Fields of a record type are accessed by name and are
2383
- arranged in memory in the order specified by the record type.
2384
-
2385
- An example of a record type and its use:
2386
-
2387
- ~~~~
2388
- type point = {x: int, y: int};
2389
- let p: point = {x: 10, y: 11};
2390
- let px: int = p.x;
2391
- ~~~~
2392
-
2393
2364
### Tuple types
2394
2365
2395
2366
The tuple type-constructor forms a new heterogeneous product of values similar
@@ -2414,6 +2385,7 @@ let (a, b) = p;
2414
2385
assert b != ~"world";
2415
2386
~~~~
2416
2387
2388
+
2417
2389
### Vector types
2418
2390
2419
2391
The vector type-constructor represents a homogeneous array of values of a
@@ -2448,16 +2420,88 @@ All accessible elements of a vector are always initialized, and access to a
2448
2420
vector is always bounds-checked.
2449
2421
2450
2422
2423
+ ### Structure types
2424
+
2425
+ A ` struct ` * type* is a heterogeneous product of other types, called the * fields* of the type.
2426
+ ^[ ` struct ` types are analogous ` struct ` types in C,
2427
+ the * record* types of the ML family,
2428
+ or the * structure* types of the Lisp family.]
2429
+
2430
+ New instances of a ` struct ` can be constructed with a [ struct expression] ( #struct-expressions ) .
2431
+
2432
+ The memory order of fields in a ` struct ` is given by the item defining it.
2433
+ Fields may be given in any order in a corresponding struct * expression* ;
2434
+ the resulting ` struct ` value will always be laid out in memory in the order specified by the corresponding * item* .
2435
+
2436
+ The fields of a ` struct ` may be qualified by [ visibility modifiers] ( #visibility-modifiers ) ,
2437
+ to restrict access to implementation-private data in a structure.
2438
+
2439
+
2451
2440
### Enumerated types
2452
2441
2453
- An * enumerated type* is a nominal, heterogeneous disjoint union type.^[ The
2454
- ` enum ` type is analogous to a ` data ` constructor declaration in ML or a * pick
2455
- ADT* in Limbo.] An [ ` enum ` * item* ] ( #enumerations ) consists of a number of
2456
- * constructors* , each of which is independently named and takes an optional
2457
- tuple of arguments.
2442
+ An * enumerated type* is a nominal, heterogeneous disjoint union type,
2443
+ denoted by the name of an [ ` enum ` item] ( #enumerations ) .
2444
+ ^[ The ` enum ` type is analogous to a ` data ` constructor declaration in ML,
2445
+ or a * pick ADT* in Limbo.]
2446
+
2447
+ An [ ` enum ` item] ( #enumerations ) declares both the type and a number of * variant constructors* ,
2448
+ each of which is independently named and takes an optional tuple of arguments.
2449
+
2450
+ New instances of an ` enum ` can be constructed by calling one of the variant constructors,
2451
+ in a [ call expression] ( #call-expressions ) .
2452
+
2453
+ Any ` enum ` value consumes as much memory as the largest variant constructor for its corresponding ` enum ` type.
2454
+
2455
+ Enum types cannot be denoted * structurally* as types,
2456
+ but must be denoted by named reference to an [ ` enum ` item] ( #enumerations ) .
2457
+
2458
+
2459
+ ### Recursive types
2460
+
2461
+ Nominal types -- [ enumerations] ( #enumerated-types ) and [ structures] ( #structure-types ) -- may be recursive.
2462
+ That is, each ` enum ` constructor or ` struct ` field may refer, directly or indirectly, to the enclosing ` enum ` or ` struct ` type itself.
2463
+ Such recursion has restrictions:
2464
+
2465
+ * Recursive types must include a nominal type in the recursion
2466
+ (not mere [ type definitions] ( #type-definitions ) ,
2467
+ or other structural types such as [ vectors] ( #vector-types ) or [ tuples] ( #tuple-types ) ).
2468
+ * A recursive ` enum ` item must have at least one non-recursive constructor
2469
+ (in order to give the recursion a basis case).
2470
+ * The size of a recursive type must be finite;
2471
+ in other words the recursive fields of the type must be [ pointer types] ( #pointer-types ) .
2472
+ * Recursive type definitions can cross module boundaries, but not module * visibility* boundaries,
2473
+ or crate boundaries (in order to simplify the module system and type checker).
2474
+
2475
+ An example of a * recursive* type and its use:
2476
+
2477
+ ~~~~
2478
+ enum List<T> {
2479
+ Nil,
2480
+ Cons(T, @List<T>)
2481
+ }
2482
+
2483
+ let a: List<int> = Cons(7, @Cons(13, @Nil));
2484
+ ~~~~
2485
+
2486
+
2487
+ ### Record types
2488
+
2489
+ > ** Note:** Records are not nominal types, thus do not directly support recursion, visibility control,
2490
+ > out-of-order field initialization, or coherent trait implementation.
2491
+ > Records are therefore deprecared and will be removed in future versions of Rust.
2492
+ > [ Structure types] ( #structure-types ) should be used instead.
2493
+
2494
+ The record type-constructor forms a new heterogeneous product of values.
2495
+ Fields of a record type are accessed by name and are arranged in memory in the order specified by the record type.
2496
+
2497
+ An example of a record type and its use:
2498
+
2499
+ ~~~~
2500
+ type Point = {x: int, y: int};
2501
+ let p: Point = {x: 10, y: 11};
2502
+ let px: int = p.x;
2503
+ ~~~~
2458
2504
2459
- Enumerated types cannot be denoted * structurally* as types, but must be
2460
- denoted by named reference to an [ * enumeration* item] ( #enumerations ) .
2461
2505
2462
2506
### Pointer types
2463
2507
@@ -2507,6 +2551,7 @@ Raw pointers (`*`)
2507
2551
they exist to support interoperability with foreign code,
2508
2552
and writing performance-critical or low-level functions.
2509
2553
2554
+
2510
2555
### Function types
2511
2556
2512
2557
The function type-constructor ` fn ` forms new function types. A function type
@@ -2553,10 +2598,6 @@ fn main() {
2553
2598
In this example, the trait ` printable ` occurs as a type in both the type signature of
2554
2599
` print ` , and the cast expression in ` main ` .
2555
2600
2556
- ### Struct types
2557
-
2558
- Every struct item defines a type.
2559
-
2560
2601
### Type parameters
2561
2602
2562
2603
Within the body of an item that has type parameter declarations, the names of its type parameters are types:
0 commit comments