@@ -211,7 +211,7 @@ The keywords in [source files](#source-files) are the following strings:
211
211
~~~~~~~~ {.keyword}
212
212
again assert
213
213
break
214
- check class const copy
214
+ check const copy
215
215
drop
216
216
else enum export extern
217
217
fail false fn for
@@ -220,6 +220,7 @@ let log loop
220
220
match mod mut
221
221
pure
222
222
return
223
+ struct
223
224
true trait type
224
225
unchecked unsafe
225
226
while
@@ -1097,151 +1098,6 @@ enum list<T> {
1097
1098
let a: list<int> = cons(7, @cons(13, @nil));
1098
1099
~~~~
1099
1100
1100
- ### Classes
1101
-
1102
- A _ class_ is a named record type that collects together fields and
1103
- methods. It must have a _ constructor_ (a function called ` new ` that
1104
- returns a new instance of the class), and may have a destructor (a
1105
- nullary function called ` drop ` that executes before the memory manager
1106
- frees the memory for a given class). For historical reasons, we may
1107
- call a class with a destructor and a single field a "resource".
1108
-
1109
- A _ class item_ declares a class type:
1110
-
1111
- ~~~~
1112
- class file_descriptor {
1113
- let fd: libc::c_int;
1114
- new(fd: libc::c_int) { self.fd = fd; }
1115
- drop { libc::close(self.fd); }
1116
- }
1117
- ~~~~
1118
-
1119
- Calling the ` file_descriptor ` constructor function on an integer will
1120
- produce a value with the ` file_descriptor ` type.
1121
-
1122
- _ Fields_ are immutable by default, so instances of ` file_descriptor `
1123
- can't have their ` fd ` fields reassigned. A mutable field declaration
1124
- looks like:
1125
-
1126
- ~~~~
1127
- let mut fd: libc::c_int;
1128
- ~~~~
1129
-
1130
- The only exception is that the body of the class constructor begins
1131
- with all the class's fields uninitialized, and is allowed to -- in
1132
- fact, must -- initialize all the fields. The compiler enforces this
1133
- invariant.
1134
-
1135
- Usually, the class constructor stores its argument or arguments in the
1136
- class's named fields. In this case, the ` file_descriptor ` 's data field
1137
- would be accessed like ` f.fd ` , if ` f ` is a value of type
1138
- ` file_descriptor ` . By default, class fields are _ public_ : they can be
1139
- accessed both from methods inside the class, and code outside the
1140
- class. Classes can also have private fields:
1141
-
1142
- ~~~~
1143
- class file_descriptor {
1144
- let fd: *libc::FILE;
1145
- new(fd: *libc::FILE) {
1146
- self.fd = fd; self.name = none;
1147
- }
1148
- priv {
1149
- let mut name: option<~str>;
1150
- }
1151
- fn get_name() -> ~str {
1152
- match self.name {
1153
- none => fail ~"File has no name!",
1154
- some(n) => n
1155
- }
1156
- }
1157
- }
1158
- ~~~~
1159
-
1160
- Private fields are instance-private: methods in a class ` C ` can access
1161
- ` self ` 's private fields, but not private fields of other values of
1162
- type ` C ` . Code outside a class can't access any private fields.
1163
-
1164
- A class item may contain _ methods_ , which take an implicit ` self `
1165
- argument:
1166
-
1167
- ~~~~
1168
- class file_descriptor {
1169
- let fd: *libc::FILE;
1170
- new(fd: *libc::FILE) { self.fd = fd; }
1171
- fn flush() {
1172
- libc::fflush(self.fd);
1173
- }
1174
- }
1175
- ~~~~
1176
-
1177
- In this case, ``` open ``` is a nullary method that calls the
1178
- ``` fopen ``` function, defined in another library, on the ``` fd ```
1179
- field. As in this example, methods must refer to their self's fields
1180
- as fields of ``` self ``` ; bare references to ``` fd ``` can't
1181
- occur. Methods can be public or private; just like fields, they are
1182
- public by default and private if enclosed in a ` priv ` section.
1183
-
1184
- Classes may be polymorphic:
1185
-
1186
- ~~~~
1187
- class file<A: copy> {
1188
- let data: A;
1189
- let fd: *libc::FILE;
1190
- new(data: A, fd: *libc::FILE) { self.data = data; self.fd = fd; }
1191
- }
1192
- ~~~~
1193
-
1194
- Methods may also be polymorphic, and can have additional type
1195
- parameters other than those bound in the class:
1196
-
1197
- ~~~~
1198
- class file<A: copy> {
1199
- let data: A;
1200
- let fd: *libc::FILE;
1201
- new(fd: *libc::FILE, data: A) { self.fd = fd; self.data = data; }
1202
- fn map_data<B>(f: fn(A) -> B) -> B {
1203
- f(self.data)
1204
- }
1205
- }
1206
- ~~~~
1207
-
1208
- Classes do not support inheritance, except through traits. As a
1209
- result, all class method dispatch is static (non-virtual).
1210
-
1211
- A class may implement a trait (see [ traits] ( #traits ) ):
1212
-
1213
- ~~~~
1214
- trait to_str {
1215
- fn to_str() -> ~str;
1216
- }
1217
-
1218
- class file : to_str {
1219
- let fd: *libc::FILE;
1220
- new(fd: *libc::FILE) { self.fd = fd; }
1221
- fn to_str() -> ~str { ~"a file" }
1222
- }
1223
- ~~~~
1224
-
1225
- The syntax ` class file: to_str ` is pronounced "class ` file `
1226
- implements trait ` to_str ` ".
1227
-
1228
- Class instances may be allocated on the stack, in the exchange heap,
1229
- or on the task heap. A value with a class type ``` C ``` has a
1230
- noncopyable [ type kind] ( #type-kinds ) if ``` C ``` has a destructor, and
1231
- thus may not be copied. Class types that don't have destructors may be
1232
- copied if all their fields are copyable.
1233
-
1234
- The semantics guarantee that for each constructed resource value, the
1235
- destructor will run once: when the value is disposed of (barring
1236
- drastic program termination that somehow prevents unwinding from
1237
- taking place). For stack-allocated values, disposal happens when the
1238
- value goes out of scope. For values in shared boxes, it happens when
1239
- the reference count of the box reaches zero.
1240
-
1241
- The order of fields in a class instance is significant; its runtime
1242
- representation is the same as that of a record with identical fields
1243
- laid out in the same order.
1244
-
1245
1101
### Traits
1246
1102
1247
1103
A _ trait item_ describes a set of method types. [ _ implementation
@@ -1348,7 +1204,7 @@ trait. The methods in such an implementation can only be used
1348
1204
statically (as direct calls on the values of the type that the
1349
1205
implementation targets). In such an implementation, the ` of ` clause is
1350
1206
not given, and the name is mandatory. Such implementations are
1351
- limited to nominal types (enums, classes ) and the implementation must
1207
+ limited to nominal types (enums, structs ) and the implementation must
1352
1208
appear in the same module or a sub-module as the receiver type.
1353
1209
1354
1210
_ When_ a trait is specified, all methods declared as part of the
@@ -2744,9 +2600,9 @@ fn main() {
2744
2600
In this example, the trait ` printable ` occurs as a type in both the type signature of
2745
2601
` print ` , and the cast expression in ` main ` .
2746
2602
2747
- ### Class types
2603
+ ### Struct types
2748
2604
2749
- Every class item defines a type. See [ classes ] ( #classes ) .
2605
+ Every struct item defines a type.
2750
2606
2751
2607
### Type parameters
2752
2608
@@ -2766,7 +2622,7 @@ type `~[B]`, a vector type with element type `B`.
2766
2622
2767
2623
### Self type
2768
2624
2769
- The special type ` self ` has a meaning within methods inside a class or
2625
+ The special type ` self ` has a meaning within methods inside an
2770
2626
impl item. It refers to the type of the implicit ` self ` argument. For
2771
2627
example, in:
2772
2628
@@ -2781,19 +2637,7 @@ impl ~str: printable {
2781
2637
~~~~~~
2782
2638
2783
2639
` self ` refers to the value of type ` str ` that is the receiver for a
2784
- call to the method ` to_str ` . Similarly, in a class declaration:
2785
-
2786
- ~~~~~~
2787
- class cat {
2788
- let mut meows: uint;
2789
- new() { self.meows = 0; }
2790
- fn meow() { self.meows = self.meows + 1; }
2791
- }
2792
- ~~~~~~
2793
-
2794
- ` self ` refers to the class instance that is the receiver of the method
2795
- (except in the constructor ` new ` , where ` self ` is the class instance
2796
- that the constructor implicitly returns).
2640
+ call to the method ` to_str ` .
2797
2641
2798
2642
## Type kinds
2799
2643
0 commit comments