1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-23 05:54:59 +02:00

Merge pull request #143 from fbornhofen/speaker-notes-copy-2

Rephrase copy/clone speaker notes
This commit is contained in:
Martin Geisler 2023-01-10 17:47:27 +01:00 committed by GitHub
commit 3eee86f76d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,13 +35,14 @@ fn main() {
Copying and cloning are not the same thing:
* Copying refers to bitwise copies of memory regions and does not work on arbitrary objects.
* Copying does not allow for custom logic (unlike copy constructors in C++).
* Cloning is a more general operation and also allows for custom behavior by implementing the `Clone` trait.
* Copying does not work on types that implement the `Drop` trait.
In the above example, try the following:
* What happens when you add a `String` field to `struct Point`?
* Does it work when you remove `Copy` from the `derive` attribute?
* After removing `Copy`, can you still print `p1` after the move?
* Add a `String` field to `struct Point`. It will not compile because `String` is not a `Copy` type.
* Remove `Copy` from the `derive` attribute. The compiler error is now in the `println!` for `p1`.
* Show that it works if you clone `p1` instead.
</details>