Skip to content

Commit 9cba4af

Browse files
committed
manual: describe structs, split out handling of recursion from enums, deprecate records.
1 parent 0673b49 commit 9cba4af

File tree

1 file changed

+100
-59
lines changed

1 file changed

+100
-59
lines changed

doc/rust.md

Lines changed: 100 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ There are several kinds of item:
705705
* [modules](#modules)
706706
* [functions](#functions)
707707
* [type definitions](#type-definitions)
708+
* [structures](#structures)
708709
* [enumerations](#enumerations)
709710
* [constants](#constants)
710711
* [traits](#traits)
@@ -1111,49 +1112,35 @@ that are composite records, each containing two unsigned 8-bit integers
11111112
accessed through the components `x` and `y`, and laid out in memory with the
11121113
`x` component preceding the `y` component.
11131114

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
11201116

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`.
11241118

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:
11341120

1121+
~~~~
1122+
struct Point {x: int, y: int}
1123+
let p = Point {x: 10, y: 11};
1124+
let px: int = p.x;
1125+
~~~~
11351126

1136-
An example of an `enum` item and its use:
1127+
### Enumerations
11371128

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.
11431131

1144-
let mut a: animal = dog;
1145-
a = cat;
1146-
~~~~
1132+
Enumerations are declared with the keyword `enum`.
11471133

1148-
An example of a *recursive* `enum` item and its use:
1134+
An example of an `enum` item and its use:
11491135

11501136
~~~~
1151-
enum list<T> {
1152-
nil,
1153-
cons(T, @list<T>)
1137+
enum Animal {
1138+
Dog,
1139+
Cat
11541140
}
11551141
1156-
let a: list<int> = cons(7, @cons(13, @nil));
1142+
let mut a: Animal = Dog;
1143+
a = Cat;
11571144
~~~~
11581145

11591146
### Constants
@@ -2374,22 +2361,6 @@ A value of type `~str` is a Unicode string, represented as a vector of 8-bit
23742361
unsigned bytes holding a sequence of UTF-8 codepoints.
23752362

23762363

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-
23932364
### Tuple types
23942365

23952366
The tuple type-constructor forms a new heterogeneous product of values similar
@@ -2414,6 +2385,7 @@ let (a, b) = p;
24142385
assert b != ~"world";
24152386
~~~~
24162387

2388+
24172389
### Vector types
24182390

24192391
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
24482420
vector is always bounds-checked.
24492421

24502422

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+
24512440
### Enumerated types
24522441

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+
~~~~
24582504

2459-
Enumerated types cannot be denoted *structurally* as types, but must be
2460-
denoted by named reference to an [*enumeration* item](#enumerations).
24612505

24622506
### Pointer types
24632507

@@ -2507,6 +2551,7 @@ Raw pointers (`*`)
25072551
they exist to support interoperability with foreign code,
25082552
and writing performance-critical or low-level functions.
25092553

2554+
25102555
### Function types
25112556

25122557
The function type-constructor `fn` forms new function types. A function type
@@ -2553,10 +2598,6 @@ fn main() {
25532598
In this example, the trait `printable` occurs as a type in both the type signature of
25542599
`print`, and the cast expression in `main`.
25552600

2556-
### Struct types
2557-
2558-
Every struct item defines a type.
2559-
25602601
### Type parameters
25612602

25622603
Within the body of an item that has type parameter declarations, the names of its type parameters are types:

0 commit comments

Comments
 (0)