From 9c652d06aad01b78aa10a468cbc7ff7eda384ca0 Mon Sep 17 00:00:00 2001 From: Fabian Bornhofen Date: Thu, 12 Jan 2023 18:23:39 +0100 Subject: [PATCH 1/7] Add speaker notes for moves-function-calls.md --- src/ownership/moves-function-calls.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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. + +
From 77f24ffab7ac2c045e7aaf6084b1df3b1ed0e05c Mon Sep 17 00:00:00 2001 From: Fabian Bornhofen Date: Thu, 12 Jan 2023 19:36:40 +0100 Subject: [PATCH 2/7] Add speaker notes for lifetimes-data-structures.md --- src/ownership/lifetimes-data-structures.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ownership/lifetimes-data-structures.md b/src/ownership/lifetimes-data-structures.md index 6ce82713..527920cf 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. +* Data structures with borrowed data force users to hold on to the original data structure. This can be useful for creating lightweight views, but generally make them somewhat harder to use. +* When appropriate, consider creating data structures that own data directly. + +
From 53706056e3f53d2f79ae14f2239c4055b0cccb50 Mon Sep 17 00:00:00 2001 From: Fabian Bornhofen Date: Fri, 13 Jan 2023 12:47:01 +0100 Subject: [PATCH 3/7] Update src/ownership/lifetimes-data-structures.md Co-authored-by: Martin Geisler --- src/ownership/lifetimes-data-structures.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ownership/lifetimes-data-structures.md b/src/ownership/lifetimes-data-structures.md index 527920cf..2f9cf301 100644 --- a/src/ownership/lifetimes-data-structures.md +++ b/src/ownership/lifetimes-data-structures.md @@ -24,7 +24,7 @@ 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. -* Data structures with borrowed data force users to hold on to the original data structure. This can be useful for creating lightweight views, but generally make them somewhat harder to use. +* 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 appropriate, consider creating data structures that own data directly. From 35893e2bc6d4f22ceeae65f3f4e8a1a6316696ba Mon Sep 17 00:00:00 2001 From: Fabian Bornhofen Date: Fri, 13 Jan 2023 12:47:15 +0100 Subject: [PATCH 4/7] Update src/ownership/lifetimes-data-structures.md Co-authored-by: Martin Geisler --- src/ownership/lifetimes-data-structures.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ownership/lifetimes-data-structures.md b/src/ownership/lifetimes-data-structures.md index 2f9cf301..d6974669 100644 --- a/src/ownership/lifetimes-data-structures.md +++ b/src/ownership/lifetimes-data-structures.md @@ -25,6 +25,6 @@ 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 appropriate, consider creating data structures that own data directly. +* When possible, make data structures own their data directly. From 71c68a699613b58df68a59a615c922603422c2ed Mon Sep 17 00:00:00 2001 From: Robin Stringer Date: Fri, 13 Jan 2023 13:14:19 +0000 Subject: [PATCH 5/7] Adds speaker notes for 6.4.1. String vs str Adds speaker notes including: -Brief explanation of &str, String type -String::from and String::new -push_str method --- src/basic-syntax/string-slices.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/basic-syntax/string-slices.md b/src/basic-syntax/string-slices.md index b3b7673e..cfd490b0 100644 --- a/src/basic-syntax/string-slices.md +++ b/src/basic-syntax/string-slices.md @@ -18,3 +18,15 @@ Rust terminology: * `&str` an immutable reference to a string slice. * `String` a mutable string buffer. + +
+ +* `&str` introduces a string slice, which is an immutable reference to UTF-8 encoded string data stored in a block of memory. String literals (`”Hello”`), are stored in the program’s binary. + +* Rust’s `String` type is a wrapper around a vector of bytes. As with a `Vec`, it is mutable and owned. + +* `String::from` creates a string from a string literal; `String::new` creates a new empty string, to which string data can be added using the `to_string` method. + +* The `push_str` method appends a string slice to the string. + +
From 4c8d85dc88abbf3609afd98d9beba3a43e2806ff Mon Sep 17 00:00:00 2001 From: Yauheni Baltukha Date: Sat, 14 Jan 2023 01:45:36 +0100 Subject: [PATCH 6/7] Fix typo in rust.md --- src/memory-management/rust.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/memory-management/rust.md b/src/memory-management/rust.md index cb18e80b..be04ff7f 100644 --- a/src/memory-management/rust.md +++ b/src/memory-management/rust.md @@ -3,7 +3,7 @@ Memory management in Rust is a mix: * Safe and correct like Java, but without a garbage collector. -* Depending on which abstraction (or combonation of abstractions) you choose, can be a single unique pointer, reference counted, or atomically reference counted. +* Depending on which abstraction (or combination of abstractions) you choose, can be a single unique pointer, reference counted, or atomically reference counted. * Scope-based like C++, but the compiler enforces full adherence. * A Rust user can choose the right abstraction for the situation, some even have no cost at runtime like C. From 93b20be600166a33639ae8b9bcfda8c4515aed64 Mon Sep 17 00:00:00 2001 From: sahennenkamp <91508060+sahennenkamp@users.noreply.github.com> Date: Fri, 13 Jan 2023 14:45:47 -0800 Subject: [PATCH 7/7] Hint towards making library mut Follow up to https://github.com/google/comprehensive-rust/pull/137 Gives the student a hint that it's okay to edit variables within main. --- src/exercises/day-1/book-library.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/exercises/day-1/book-library.rs b/src/exercises/day-1/book-library.rs index cab1879f..284045aa 100644 --- a/src/exercises/day-1/book-library.rs +++ b/src/exercises/day-1/book-library.rs @@ -98,7 +98,8 @@ impl Library { // ANCHOR: main // This shows the desired behavior. Uncomment the code below and // implement the missing methods. You will need to update the -// method signatures, including the "self" parameter! +// method signatures, including the "self" parameter! You may +// also need to update the variable bindings within main. fn main() { let library = Library::new();