Skip to content

Commit d513b23

Browse files
committed
More tweaks to supertraits
1 parent b09e52c commit d513b23

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/items/traits.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ interface consists of [associated items], which come in three varieties:
99

1010
All traits define an implicit type parameter `Self` that refers to "the type
1111
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].
1414

1515
Traits are implemented for specific types through separate [implementations].
1616

@@ -26,8 +26,7 @@ Generic items may use traits as [bounds] on their type parameters.
2626
## Generic Traits
2727

2828
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].
3130

3231
```rust
3332
trait Seq<T> {
@@ -60,6 +59,8 @@ Supertraits are declared by trait bounds on the `Self` type of a trait and
6059
transitively the supertraits of the traits declared in those trait bounds. It is
6160
an error for a trait to be its own supertrait.
6261

62+
The trait with a supertrait is called a **subtrait** of its supertrait.
63+
6364
The following is an example of declaring `Shape` to be a supertrait of `Circle`.
6465

6566
```rust
@@ -89,9 +90,7 @@ trait Circle where Self: Shape {
8990
}
9091
```
9192

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.
9594

9695
```rust
9796
# trait Shape { fn area(&self) -> f64; }
@@ -103,16 +102,16 @@ fn print_area_and_radius<C: Circle>(c: C) {
103102
}
104103
```
105104

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.
107106

108107
```rust
109108
# trait Shape { fn area(&self) -> f64; }
110109
# trait Circle : Shape { fn radius(&self) -> f64; }
111110
# 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 = 0i32;
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>;
116115
let nonsense = circle.radius() * circle.area();
117116
```
118117

@@ -123,5 +122,6 @@ let nonsense = circle.radius() * circle.area();
123122
[associated items]: items/associated-items.html
124123
[method]: items/associated-items.html#methods
125124
[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

Comments
 (0)