@@ -593,7 +593,7 @@ There are several kinds of item:
593
593
* [ type definitions] ( #type-definitions )
594
594
* [ enumerations] ( #enumerations )
595
595
* [ resources] ( #resources )
596
- * [ interfaces ] ( #interfaces )
596
+ * [ traits ] ( #traits )
597
597
* [ implementations] ( #implementations )
598
598
599
599
Some items form an implicit scope for the declaration of sub-items. In other
@@ -1012,8 +1012,8 @@ parameter is given a [`copy` bound](#type-kinds).
1012
1012
fn id<T: copy>(x: T) -> T { x }
1013
1013
~~~~
1014
1014
1015
- Similarly, [ interface ] ( #interfaces ) bounds can be specified for type
1016
- parameters to allow methods of that interface to be called on values
1015
+ Similarly, [ trait ] ( #traits ) bounds can be specified for type
1016
+ parameters to allow methods with that trait to be called on values
1017
1017
of that type.
1018
1018
1019
1019
#### Extern functions
@@ -1153,11 +1153,11 @@ class file_descriptor {
1153
1153
self.fd = fd; self.name = none;
1154
1154
}
1155
1155
priv {
1156
- let mut name: option<str>;
1156
+ let mut name: option<~ str>;
1157
1157
}
1158
- fn get_name() -> str {
1158
+ fn get_name() -> ~ str {
1159
1159
alt self.name {
1160
- none { fail "File has no name!"; }
1160
+ none { fail ~ "File has no name!"; }
1161
1161
some(n) { n }
1162
1162
}
1163
1163
}
@@ -1215,17 +1215,17 @@ class file<A: copy> {
1215
1215
Classes do not support inheritance, except through traits. As a
1216
1216
result, all class method dispatch is static (non-virtual).
1217
1217
1218
- A class may implement a trait (see [ interfaces ] ( #interfaces ) ):
1218
+ A class may implement a trait (see [ traits ] ( #traits ) ):
1219
1219
1220
1220
~~~~
1221
1221
trait to_str {
1222
- fn to_str() -> str;
1222
+ fn to_str() -> ~ str;
1223
1223
}
1224
1224
1225
1225
class file : to_str {
1226
1226
let fd: *libc::FILE;
1227
1227
new(fd: *libc::FILE) { self.fd = fd; }
1228
- fn to_str() -> str { "a file" }
1228
+ fn to_str() -> ~ str { ~ "a file" }
1229
1229
}
1230
1230
~~~~
1231
1231
@@ -1249,9 +1249,9 @@ The order of fields in a class instance is significant; its runtime
1249
1249
representation is the same as that of a record with identical fields
1250
1250
laid out in the same order.
1251
1251
1252
- ### Interfaces
1252
+ ### Traits
1253
1253
1254
- An _ interface item_ describes a set of method types. _ [ implementation
1254
+ A _ trait item_ describes a set of method types. _ [ implementation
1255
1255
items] ( #implementations ) _ can be used to provide implementations of
1256
1256
those methods for a specific type.
1257
1257
@@ -1265,12 +1265,12 @@ iface shape {
1265
1265
}
1266
1266
~~~~
1267
1267
1268
- This defines an interface with two methods. All values which have
1269
- [ implementations] ( #implementations ) of this interface in scope can
1268
+ This defines a trait with two methods. All values that have
1269
+ [ implementations] ( #implementations ) of this trait in scope can
1270
1270
have their ` draw ` and ` bounding_box ` methods called, using
1271
1271
` value.bounding_box() ` [ syntax] ( #field-expressions ) .
1272
1272
1273
- Type parameters can be specified for an interface to make it generic.
1273
+ Type parameters can be specified for a trait to make it generic.
1274
1274
These appear after the name, using the same syntax used in [ generic
1275
1275
functions] ( #generic-functions ) .
1276
1276
@@ -1282,10 +1282,10 @@ iface seq<T> {
1282
1282
}
1283
1283
~~~~
1284
1284
1285
- Generic functions may use interfaces as bounds on their type
1286
- parameters. This will have two effects: only types that implement the
1287
- interface can be used to instantiate the parameter, and within the
1288
- generic function, the methods of the interface can be called on values
1285
+ Generic functions may use traits as bounds on their type
1286
+ parameters. This will have two effects: only types that have the trait
1287
+ may instantiate the parameter, and within the
1288
+ generic function, the methods of the trait can be called on values
1289
1289
that have the parameter's type. For example:
1290
1290
1291
1291
~~~~
@@ -1298,8 +1298,8 @@ fn draw_twice<T: shape>(surface: surface, sh: T) {
1298
1298
}
1299
1299
~~~~
1300
1300
1301
- Interface items also define a type with the same name as the
1302
- interface . Values of this type are created by
1301
+ Trait items also define a type with the same name as the
1302
+ trait . Values of this type are created by
1303
1303
[ casting] ( #type-cast-expressions ) values (of a type for which an
1304
1304
implementation of the given interface is in scope) to the interface
1305
1305
type.
@@ -1321,7 +1321,7 @@ instantiate type parameters that are bounded on their interface.
1321
1321
### Implementations
1322
1322
1323
1323
An _ implementation item_ provides an implementation of an
1324
- [ interface] ( #interfaces ) for a type.
1324
+ [ interface] ( #traits ) for a type.
1325
1325
1326
1326
~~~~
1327
1327
# type point = {x: float, y: float};
@@ -1682,7 +1682,7 @@ When the type of the expression to the left of the dot is a boxed
1682
1682
record, it is automatically derferenced to make the field access
1683
1683
possible.
1684
1684
1685
- Field access syntax is overloaded for [ interface method] ( #interfaces )
1685
+ Field access syntax is overloaded for [ trait method] ( #traits )
1686
1686
access. When no matching field is found, or the expression to the left
1687
1687
of the dot is not a (boxed) record, an
1688
1688
[ implementation] ( #implementations ) that matches this type and the
@@ -2067,11 +2067,10 @@ conditional expression evaluates to `false`, the `while` expression completes.
2067
2067
An example:
2068
2068
2069
2069
~~~~
2070
- # let mut i = 0;
2071
- # let println = io::println;
2070
+ let mut i = 0;
2072
2071
2073
2072
while i < 10 {
2074
- println(~"hello\n");
2073
+ io:: println(~"hello\n");
2075
2074
i = i + 1;
2076
2075
}
2077
2076
~~~~
@@ -2758,6 +2757,84 @@ let bo: binop = add;
2758
2757
x = bo(5,7);
2759
2758
~~~~~~~~
2760
2759
2760
+ ### Trait types
2761
+
2762
+ Every trait item (see [ traits] ( #traits ) ) defines a type with the same name
2763
+ as the trait. For a trait ` T ` , cast expressions introduce values of type ` T ` :
2764
+
2765
+ ~~~~~~~~
2766
+ // doc extractor doesn't recognize trait -- fix it
2767
+ iface printable {
2768
+ fn to_str() -> ~str;
2769
+ }
2770
+
2771
+ impl of printable for ~str {
2772
+ fn to_str() -> ~str { self }
2773
+ }
2774
+
2775
+ fn print(a: printable) {
2776
+ io::println(a.to_str());
2777
+ }
2778
+
2779
+ fn main() {
2780
+ print(~"meow" as printable);
2781
+ }
2782
+ ~~~~~~~~
2783
+
2784
+ In this example, the trait ` printable ` occurs as a type in both the type signature of
2785
+ ` print ` , and the cast expression in ` main ` .
2786
+
2787
+ ### Class types
2788
+
2789
+ Every class item defines a type. See [ classes] ( #classes ) .
2790
+
2791
+ ### Type parameters
2792
+
2793
+ Within the body of an item that has type parameter declarations, the names of its type parameters are types:
2794
+
2795
+ ~~~~~~~
2796
+ fn map<A: copy, B: copy>(f: fn(A) -> B, xs: ~[A]) -> ~[B] {
2797
+ if xs.len() == 0 { ret ~[]; }
2798
+ let first: B = f(xs[0]);
2799
+ let rest: ~[B] = map(f, xs.slice(1, xs.len()));
2800
+ ret ~[first] + rest;
2801
+ }
2802
+ ~~~~~~~
2803
+
2804
+ Here, ` first ` has type ` B ` , referring to ` map ` 's ` B ` type parameter; and ` rest ` has
2805
+ type ` ~[B] ` , a vector type with element type ` B ` .
2806
+
2807
+ ### Self type
2808
+
2809
+ The special type ` self ` has a meaning within methods inside a class or
2810
+ impl item. It refers to the type of the implicit ` self ` argument. For
2811
+ example, in:
2812
+
2813
+ ~~~~~~
2814
+ iface printable {
2815
+ fn to_str() -> ~str;
2816
+ }
2817
+
2818
+ impl of printable for ~str {
2819
+ fn to_str() -> ~str { self }
2820
+ }
2821
+ ~~~~~~
2822
+
2823
+ ` self ` refers to the value of type ` str ` that is the receiver for a
2824
+ call to the method ` to_str ` . Similarly, in a class declaration:
2825
+
2826
+ ~~~~~~
2827
+ class cat {
2828
+ let mut meows: uint;
2829
+ new() { self.meows = 0; }
2830
+ fn meow() { self.meows = self.meows + 1; }
2831
+ }
2832
+ ~~~~~~
2833
+
2834
+ ` self ` refers to the class instance that is the receiver of the method
2835
+ (except in the constructor ` new ` , where ` self ` is the class instance
2836
+ that the constructor implicitly returns).
2837
+
2761
2838
## Type kinds
2762
2839
2763
2840
Types in Rust are categorized into three kinds, based on whether they
0 commit comments