@@ -1346,6 +1346,8 @@ vtable when the trait is used as a [trait object](#trait-objects).
1346
1346
Traits are implemented for specific types through separate
1347
1347
[ implementations] ( #implementations ) .
1348
1348
1349
+ Consider the following trait:
1350
+
1349
1351
```
1350
1352
# type Surface = i32;
1351
1353
# type BoundingBox = i32;
@@ -1360,6 +1362,20 @@ This defines a trait with two methods. All values that have
1360
1362
` draw ` and ` bounding_box ` methods called, using ` value.bounding_box() `
1361
1363
[ syntax] ( #method-call-expressions ) .
1362
1364
1365
+ Traits can include default implementations of methods, as in:
1366
+
1367
+ ```
1368
+ trait Foo {
1369
+ fn bar(&self);
1370
+
1371
+ fn baz(&self) { println!("We called baz."); }
1372
+ }
1373
+ ```
1374
+
1375
+ Here the ` baz ` method has a default implementation, so types that implement
1376
+ ` Foo ` need only implement ` bar ` . It is also possible for implementing types
1377
+ to override a method that has a default implementation.
1378
+
1363
1379
Type parameters can be specified for a trait to make it generic. These appear
1364
1380
after the trait name, using the same syntax used in [ generic
1365
1381
functions] ( #generic-functions ) .
@@ -1372,6 +1388,35 @@ trait Seq<T> {
1372
1388
}
1373
1389
```
1374
1390
1391
+ It is also possible to define associated types for a trait. Consider the
1392
+ following example of a ` Container ` trait. Notice how the type is available
1393
+ for use in the method signatures:
1394
+
1395
+ ```
1396
+ trait Container {
1397
+ type E;
1398
+ fn empty() -> Self;
1399
+ fn insert(&mut self, Self::E);
1400
+ }
1401
+ ```
1402
+
1403
+ In order for a type to implement this trait, it must not only provide
1404
+ implementations for every method, but it must specify the type ` E ` . Here's
1405
+ an implementation of ` Container ` for the standard library type ` Vec ` :
1406
+
1407
+ ```
1408
+ # trait Container {
1409
+ # type E;
1410
+ # fn empty() -> Self;
1411
+ # fn insert(&mut self, Self::E);
1412
+ # }
1413
+ impl<T> Container for Vec<T> {
1414
+ type E = T;
1415
+ fn empty() -> Vec<T> { Vec::new() }
1416
+ fn insert(&mut self, x: T) { self.push(x); }
1417
+ }
1418
+ ```
1419
+
1375
1420
Generic functions may use traits as _ bounds_ on their type parameters. This
1376
1421
will have two effects: only types that have the trait may instantiate the
1377
1422
parameter, and within the generic function, the methods of the trait can be
@@ -3470,13 +3515,21 @@ more of the closure traits:
3470
3515
3471
3516
### Trait objects
3472
3517
3473
- Every trait item (see [ traits] ( #traits ) ) defines a type with the same name as
3474
- the trait. This type is called the _ trait object_ of the trait. Trait objects
3475
- permit "late binding" of methods, dispatched using _ virtual method tables_
3476
- ("vtables"). Whereas most calls to trait methods are "early bound" (statically
3477
- resolved) to specific implementations at compile time, a call to a method on an
3478
- trait objects is only resolved to a vtable entry at compile time. The actual
3479
- implementation for each vtable entry can vary on an object-by-object basis.
3518
+ In Rust, a type like ` &SomeTrait ` or ` Box<SomeTrait> ` is called a _ trait object_ .
3519
+ Each instance of a trait object includes:
3520
+
3521
+ - a pointer to an instance of a type ` T ` that implements ` SomeTrait `
3522
+ - a _ virtual method table_ , often just called a _ vtable_ , which contains, for
3523
+ each method of ` SomeTrait ` that ` T ` implements, a pointer to ` T ` 's
3524
+ implementation (i.e. a function pointer).
3525
+
3526
+ The purpose of trait objects is to permit "late binding" of methods. A call to
3527
+ a method on a trait object is only resolved to a vtable entry at compile time.
3528
+ The actual implementation for each vtable entry can vary on an object-by-object
3529
+ basis.
3530
+
3531
+ Note that for a trait object to be instantiated, the trait must be
3532
+ _ object-safe_ . Object safety rules are defined in [ RFC 255] [ rfc255 ] .
3480
3533
3481
3534
Given a pointer-typed expression ` E ` of type ` &T ` or ` Box<T> ` , where ` T `
3482
3535
implements trait ` R ` , casting ` E ` to the corresponding pointer type ` &R ` or
0 commit comments