@@ -1167,12 +1167,12 @@ or constrained by some other [trait type](#trait-types).
1167
1167
Traits are implemented for specific types through separate [ implementations] ( #implementations ) .
1168
1168
1169
1169
~~~~
1170
- # type surface = int;
1171
- # type bounding_box = int;
1170
+ # type Surface = int;
1171
+ # type BoundingBox = int;
1172
1172
1173
- trait shape {
1174
- fn draw(surface );
1175
- fn bounding_box() -> bounding_box ;
1173
+ trait Shape {
1174
+ fn draw(Surface );
1175
+ fn bounding_box() -> BoundingBox ;
1176
1176
}
1177
1177
~~~~
1178
1178
@@ -1181,101 +1181,98 @@ All values that have [implementations](#implementations) of this trait in scope
1181
1181
using ` value.bounding_box() ` [ syntax] ( #method-call-expressions ) .
1182
1182
1183
1183
Type parameters can be specified for a trait to make it generic.
1184
- These appear after the name, using the same syntax used in [ generic
1185
- functions] ( #generic-functions ) .
1184
+ These appear after the trait name, using the same syntax used in [ generic functions] ( #generic-functions ) .
1186
1185
1187
1186
~~~~
1188
- trait seq <T> {
1187
+ trait Seq <T> {
1189
1188
fn len() -> uint;
1190
1189
fn elt_at(n: uint) -> T;
1191
1190
fn iter(fn(T));
1192
1191
}
1193
1192
~~~~
1194
1193
1195
- Generic functions may use traits as bounds on their type
1196
- parameters. This will have two effects: only types that have the trait
1197
- may instantiate the parameter, and within the
1198
- generic function, the methods of the trait can be called on values
1199
- that have the parameter's type. For example:
1194
+ Generic functions may use traits as _ bounds _ on their type parameters.
1195
+ This will have two effects: only types that have the trait may instantiate the parameter,
1196
+ and within the generic function,
1197
+ the methods of the trait can be called on values that have the parameter's type.
1198
+ For example:
1200
1199
1201
1200
~~~~
1202
- # type surface = int;
1203
- # trait shape { fn draw(surface ); }
1201
+ # type Surface = int;
1202
+ # trait Shape { fn draw(Surface ); }
1204
1203
1205
- fn draw_twice<T: shape >(surface: surface , sh: T) {
1204
+ fn draw_twice<T: Shape >(surface: Surface , sh: T) {
1206
1205
sh.draw(surface);
1207
1206
sh.draw(surface);
1208
1207
}
1209
1208
~~~~
1210
1209
1211
- Trait items also define a type with the same name as the
1212
- trait. Values of this type are created by
1213
- [ casting] ( #type-cast-expressions ) values (of a type for which an
1214
- implementation of the given trait is in scope) to the trait
1215
- type.
1210
+ Traits also define a [ type] ( #trait-types ) with the same name as the trait.
1211
+ Values of this type are created by [ casting] ( #type-cast-expressions ) pointer values
1212
+ (pointing to a type for which an implementation of the given trait is in scope)
1213
+ to pointers to the trait name, used as a type.
1216
1214
1217
1215
~~~~
1218
- # trait shape { }
1219
- # impl int: shape { }
1216
+ # trait Shape { }
1217
+ # impl int: Shape { }
1220
1218
# let mycircle = 0;
1221
1219
1222
- let myshape: shape = mycircle as shape ;
1220
+ let myshape: Shape = @ mycircle as @Shape ;
1223
1221
~~~~
1224
1222
1225
- The resulting value is a reference-counted box containing the value
1226
- that was cast along with information that identify the methods of the
1227
- implementation that was used. Values with a trait type can always
1228
- have methods from their trait called on them, and can be used to
1229
- instantiate type parameters that are bounded by their trait.
1223
+ The resulting value is a managed box containing the value that was cast,
1224
+ along with information that identify the methods of the implementation that was used.
1225
+ Values with a trait type can have [ methods called ] ( #method-call-expressions ) on them,
1226
+ for any method in the trait,
1227
+ and can be used to instantiate type parameters that are bounded by the trait.
1230
1228
1231
1229
### Implementations
1232
1230
1233
- An _ implementation item_ provides an implementation of a
1234
- [ trait] ( #traits ) for a type.
1231
+ An _ implementation_ is an item that implements a [ trait] ( #traits ) for a specific type.
1232
+
1233
+ Implementations are defined with the keyword ` impl ` .
1235
1234
1236
1235
~~~~
1237
- # type point = {x: float, y: float};
1238
- # type surface = int;
1239
- # type bounding_box = {x: float, y: float, width: float, height: float};
1240
- # trait shape { fn draw(surface); fn bounding_box() -> bounding_box ; }
1241
- # fn do_draw_circle(s: surface , c: circle ) { }
1236
+ # type Point = {x: float, y: float};
1237
+ # type Surface = int;
1238
+ # type BoundingBox = {x: float, y: float, width: float, height: float};
1239
+ # trait Shape { fn draw(surface); fn bounding_box() -> BoundingBox ; }
1240
+ # fn do_draw_circle(s: Surface , c: Circle ) { }
1242
1241
1243
- type circle = {radius: float, center: point};
1242
+ type Circle = {radius: float, center: point};
1244
1243
1245
- impl circle: shape {
1246
- fn draw(s: surface ) { do_draw_circle(s, self); }
1247
- fn bounding_box() -> bounding_box {
1244
+ impl Circle: Shape {
1245
+ fn draw(s: Surface ) { do_draw_circle(s, self); }
1246
+ fn bounding_box() -> BoundingBox {
1248
1247
let r = self.radius;
1249
1248
{x: self.center.x - r, y: self.center.y - r,
1250
1249
width: 2.0 * r, height: 2.0 * r}
1251
1250
}
1252
1251
}
1253
1252
~~~~
1254
1253
1255
- It is possible to define an implementation without referring to a
1256
- trait. The methods in such an implementation can only be used
1257
- statically (as direct calls on the values of the type that the
1258
- implementation targets). In such an implementation, the type after the colon is omitted,
1259
- and the name is mandatory. Such implementations are
1260
- limited to nominal types (enums, structs) and the implementation must
1261
- appear in the same module or a sub-module as the receiver type.
1254
+ It is possible to define an implementation without referring to a trait.
1255
+ The methods in such an implementation can only be used statically
1256
+ (as direct calls on the values of the type that the implementation targets).
1257
+ In such an implementation, the type after the colon is omitted.
1258
+ Such implementations are limited to nominal types (enums, structs),
1259
+ and the implementation must appear in the same module or a sub-module as the ` self ` type.
1262
1260
1263
- _ When _ a trait is specified, all methods declared as part of the
1264
- trait must be present, with matching types and type parameter
1265
- counts, in the implementation .
1261
+ When a trait _ is _ specified in an ` impl ` ,
1262
+ all methods declared as part of the trait must be implemented,
1263
+ with matching types and type parameter counts .
1266
1264
1267
- An implementation can take type parameters, which can be different
1268
- from the type parameters taken by the trait it implements. They
1269
- are written after the name of the implementation, or if that is not
1270
- specified, after the ` impl ` keyword.
1265
+ An implementation can take type parameters,
1266
+ which can be different from the type parameters taken by the trait it implements.
1267
+ Implementation parameters are written after after the ` impl ` keyword.
1271
1268
1272
1269
~~~~
1273
- # trait seq <T> { }
1270
+ # trait Seq <T> { }
1274
1271
1275
- impl<T> ~[T]: seq <T> {
1272
+ impl<T> ~[T]: Seq <T> {
1276
1273
...
1277
1274
}
1278
- impl u32: seq <bool> {
1275
+ impl u32: Seq <bool> {
1279
1276
/* Treat the integer as a sequence of bits */
1280
1277
}
1281
1278
~~~~
0 commit comments