1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-26 01:04:35 +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 and cloning are not the same thing:
* Copying refers to bitwise copies of memory regions and does not work on arbitrary objects. * 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. * 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. * Copying does not work on types that implement the `Drop` trait.
In the above example, try the following: In the above example, try the following:
* What happens when you add a `String` field to `struct Point`? * Add a `String` field to `struct Point`. It will not compile because `String` is not a `Copy` type.
* Does it work when you remove `Copy` from the `derive` attribute? * Remove `Copy` from the `derive` attribute. The compiler error is now in the `println!` for `p1`.
* After removing `Copy`, can you still print `p1` after the move? * Show that it works if you clone `p1` instead.
</details> </details>