1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-24 16:42:36 +02:00

Add discussion points for traits (#180)

* Some discussion points for traits
* Add answers
This commit is contained in:
rbehjati 2023-01-26 09:14:05 +00:00 committed by GitHub
parent ec01563bd6
commit f15fc1a0b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -28,3 +28,15 @@ fn main() {
println!("Exiting main");
}
```
<details>
Discussion points:
* Why does not `Drop::drop` take `self`?
* Short-answer: If it did, `std::mem::drop` would be called at the end of
the block, resulting in another call to `Drop::drop`, and a stack
overflow!
* Try replacing `drop(a)` with `a.drop()`.
</details>

View File

@ -20,3 +20,19 @@ fn main() {
println!("{:?} + {:?} = {:?}", p1, p2, p1 + p2);
}
```
<details>
Discussion points:
* You could implement `Add` for `&Point`. In which situations is that useful?
* Answer: `Add:add` consumes `self`. If type `T` for which you are
overloading the operator is not `Copy`, you should consider overloading
the operator for `&T` as well. This avoids unnecessary cloning on the
call site.
* Why is `Output` an associated type? Could it be made a type parameter?
* Short answer: Type parameters are controlled by the caller, but
associated types (like `Output`) are controlled by the implementor of a
trait.
</details>