mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-25 16:54:32 +02:00
Add discussion points for traits (#180)
* Some discussion points for traits * Add answers
This commit is contained in:
parent
ec01563bd6
commit
f15fc1a0b8
@ -28,3 +28,15 @@ fn main() {
|
|||||||
println!("Exiting 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>
|
@ -20,3 +20,19 @@ fn main() {
|
|||||||
println!("{:?} + {:?} = {:?}", p1, p2, p1 + p2);
|
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>
|
Loading…
x
Reference in New Issue
Block a user