mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-22 07:47:44 +02:00
Add code for speaker notes in trait inheritance (#643)
Give code samples of trait inheritance and a blanket implementation. Signed-off-by: Edward Liaw <edliaw@google.com>
This commit is contained in:
parent
caeabdae3e
commit
8406697449
@ -35,7 +35,26 @@ fn main() {
|
|||||||
* Move method `not_equal` to a new trait `NotEqual`.
|
* Move method `not_equal` to a new trait `NotEqual`.
|
||||||
|
|
||||||
* Make `NotEqual` a super trait for `Equal`.
|
* Make `NotEqual` a super trait for `Equal`.
|
||||||
|
```rust,editable,compile_fail
|
||||||
|
trait NotEqual: Equals {
|
||||||
|
fn not_equal(&self, other: &Self) -> bool {
|
||||||
|
!self.equal(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
* Provide a blanket implementation of `NotEqual` for `Equal`.
|
* Provide a blanket implementation of `NotEqual` for `Equal`.
|
||||||
|
```rust,editable,compile_fail
|
||||||
|
trait NotEqual {
|
||||||
|
fn not_equal(&self, other: &Self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> NotEqual for T where T: Equals {
|
||||||
|
fn not_equal(&self, other: &Self) -> bool {
|
||||||
|
!self.equal(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
* With the blanket implementation, you no longer need `NotEqual` as a super trait for `Equal`.
|
* With the blanket implementation, you no longer need `NotEqual` as a super trait for `Equal`.
|
||||||
|
|
||||||
</details>
|
</details>
|
Loading…
x
Reference in New Issue
Block a user