diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-08-05 15:31:19 -0400 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-08-05 15:31:19 -0400 |
| commit | de98a0b8fec7fb4539307caea667112fe107bfce (patch) | |
| tree | c86774e7099d59b909eebdf5712f753f0b8a3ecf /src | |
| parent | d03456183e85fe7bd465bbe7c8f67885a2528444 (diff) | |
| download | rust-de98a0b8fec7fb4539307caea667112fe107bfce.tar.gz rust-de98a0b8fec7fb4539307caea667112fe107bfce.zip | |
Add an example to Trait section of reference
Fixes #26115
Diffstat (limited to 'src')
| -rw-r--r-- | src/doc/reference.md | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index fdb45c32a1d..e905ed917d7 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -1501,7 +1501,29 @@ have an implementation for `Shape`. Multiple supertraits are separated by `+`, `trait Circle : Shape + PartialEq { }`. In an implementation of `Circle` for a given type `T`, methods can refer to `Shape` methods, since the typechecker checks that any type with an implementation of `Circle` also has an -implementation of `Shape`. +implementation of `Shape`: + +```rust +struct Foo; + +trait Shape { fn area(&self) -> f64; } +trait Circle : Shape { fn radius(&self) -> f64; } +# impl Shape for Foo { +# fn area(&self) -> f64 { +# 0.0 +# } +# } +impl Circle for Foo { + fn radius(&self) -> f64 { + println!("calling area: {}", self.area()); + + 0.0 + } +} + +let c = Foo; +c.radius(); +``` In type-parameterized functions, methods of the supertrait may be called on values of subtrait-bound type parameters. Referring to the previous example of |
