diff --git a/src/ownership/lifetimes-data-structures.md b/src/ownership/lifetimes-data-structures.md index 6ce82713..d6974669 100644 --- a/src/ownership/lifetimes-data-structures.md +++ b/src/ownership/lifetimes-data-structures.md @@ -20,3 +20,11 @@ fn main() { } ``` +
+ +* 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. + +
diff --git a/src/ownership/moves-function-calls.md b/src/ownership/moves-function-calls.md index 84316e49..8b0f80ac 100644 --- a/src/ownership/moves-function-calls.md +++ b/src/ownership/moves-function-calls.md @@ -14,3 +14,12 @@ fn main() { // say_hello(name); } ``` + +
+ +* 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. + +