1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-02-18 09:40:22 +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:
Edward Liaw 2023-05-17 02:02:02 -07:00 committed by GitHub
parent caeabdae3e
commit 8406697449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,7 +35,26 @@ fn main() {
* Move method `not_equal` to a new trait `NotEqual`.
* 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`.
```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`.
</details>