@@ -9,8 +9,8 @@ interface consists of [associated items], which come in three varieties:
9
9
10
10
All traits define an implicit type parameter ` Self ` that refers to "the type
11
11
that is implementing this interface". Traits may also contain additional type
12
- parameters. These type parameters ( including ` Self ` ) may be constrained by
13
- other traits and so forth [ as usual] .
12
+ parameters. These type parameters, including ` Self ` , may be constrained by
13
+ other traits and so forth [ as usual] [ generics ] .
14
14
15
15
Traits are implemented for specific types through separate [ implementations] .
16
16
@@ -26,8 +26,7 @@ Generic items may use traits as [bounds] on their type parameters.
26
26
## Generic Traits
27
27
28
28
Type parameters can be specified for a trait to make it generic. These appear
29
- after the trait name, using the same syntax used in [ generic
30
- functions] ( items/functions.html#generic-functions ) .
29
+ after the trait name, using the same syntax used in [ generic functions] .
31
30
32
31
``` rust
33
32
trait Seq <T > {
@@ -60,6 +59,8 @@ Supertraits are declared by trait bounds on the `Self` type of a trait and
60
59
transitively the supertraits of the traits declared in those trait bounds. It is
61
60
an error for a trait to be its own supertrait.
62
61
62
+ The trait with a supertrait is called a ** subtrait** of its supertrait.
63
+
63
64
The following is an example of declaring ` Shape ` to be a supertrait of ` Circle ` .
64
65
65
66
``` rust
@@ -89,9 +90,7 @@ trait Circle where Self: Shape {
89
90
}
90
91
```
91
92
92
- In type-parameterized functions, methods of the supertrait may be called on
93
- values of subtrait-bound type parameters. Continuing the example of
94
- ` trait Circle : Shape ` :
93
+ This next example calls a supertrait method on a generic parameter.
95
94
96
95
``` rust
97
96
# trait Shape { fn area (& self ) -> f64 ; }
@@ -103,16 +102,16 @@ fn print_area_and_radius<C: Circle>(c: C) {
103
102
}
104
103
```
105
104
106
- Likewise , here is an example of calling supertrait methods on trait objects.
105
+ Similarly , here is an example of calling supertrait methods on trait objects.
107
106
108
107
``` rust
109
108
# trait Shape { fn area (& self ) -> f64 ; }
110
109
# trait Circle : Shape { fn radius (& self ) -> f64 ; }
111
110
# struct UnitCircle ;
112
- # impl Shape for i32 { fn area (& self ) -> f64 { 0.0 } }
113
- # impl Circle for i32 { fn radius (& self ) -> f64 { 0 .0 } }
114
- # let mycircle = 0 i32 ;
115
- let circle = Box :: new (mycircle ) as Box <dyn Circle >;
111
+ # impl Shape for UnitCircle { fn area (& self ) -> f64 { std :: f64 :: consts :: PI } }
112
+ # impl Circle for UnitCircle { fn radius (& self ) -> f64 { 1 .0 } }
113
+ # let circle = UnitCircle ;
114
+ let circle = Box :: new (circle ) as Box <dyn Circle >;
116
115
let nonsense = circle . radius () * circle . area ();
117
116
```
118
117
@@ -123,5 +122,6 @@ let nonsense = circle.radius() * circle.area();
123
122
[ associated items ] : items/associated-items.html
124
123
[ method ] : items/associated-items.html#methods
125
124
[ implementations ] : items/implementations.html
126
- [ as usual ] : items/generics.html
127
- [ where clauses ] : items/generics.html#where-clauses
125
+ [ generics ] : items/generics.html
126
+ [ where clauses ] : items/generics.html#where-clauses
127
+ [ generic functions ] : items/functions.html#generic-functions
0 commit comments