1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-10 00:44:21 +02:00

Merge pull request #164 from fbornhofen/speaker-notes-move

Add speaker notes for moves-function-calls.md
This commit is contained in:
Fabian Bornhofen 2023-01-16 11:34:02 +01:00 committed by GitHub
commit 61732adadd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -20,3 +20,11 @@ fn main() {
}
```
<details>
* In the above example, the annotation on `Highlight` enforces that the data underlying the contained `&str` lives at least as long as any instance of `Highlight` that uses that data.
* If `text` is consumed before the end of the lifetime of `fox` (or `dog`), the borrow checker throws an error.
* Types with borrowed data force users to hold on to the original data. This can be useful for creating lightweight views, but it generally makes them somewhat harder to use.
* When possible, make data structures own their data directly.
</details>

View File

@ -14,3 +14,12 @@ fn main() {
// say_hello(name);
}
```
<details>
* With the first call to `say_hello`, `main` gives up ownership of `name`. Afterwards, `name` cannot be used anymore within `main`.
* `main` can retain ownership if it passes `name` as a reference (`&name`) and if `say_hello` accepts a reference as a parameter.
* Alternatively, `main` can pass a clone of `name` in the first call (`name.clone()`).
* Rust makes it harder than C++ to inadvertently create copies by making move semantics the default, and by forcing programmers to make clones explicit.
</details>