From 54ab112f40179200c7431a8deaad4666db94303d Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Wed, 2 Nov 2016 10:47:01 -0400 Subject: [PATCH] Add caption tags since I started doing that but never fixed these --- nostarch/chapter04.md | 12 ++++++++++++ src/ch04-01-ownership.md | 8 ++++++++ src/ch04-02-references-and-borrowing.md | 2 ++ src/ch04-03-slices.md | 2 ++ 4 files changed, 24 insertions(+) diff --git a/nostarch/chapter04.md b/nostarch/chapter04.md index 3504e62..d2c8334 100644 --- a/nostarch/chapter04.md +++ b/nostarch/chapter04.md @@ -274,8 +274,10 @@ is the memory that holds the contents, and this is on the heap. String in memory + Figure 4-1: Representation in memory of a `String` holding the value `"hello"` bound to `s1` + The length is how much memory, in bytes, the contents of the `String` is currently using. The capacity is the total amount of memory, in bytes, that the @@ -290,8 +292,10 @@ words, it looks like figure 4-2. s1 and s2 pointing to the same value + Figure 4-2: Representation in memory of the variable `s2` that has a copy of `s1`’s pointer, length and capacity + And *not* Figure 4-3, which is what memory would look like if Rust instead copied the heap data as well. If Rust did this, the operation `s2 = s1` could @@ -299,8 +303,10 @@ potentially be very expensive if the data on the heap was large. s1 and s2 to two places + Figure 4-3: Another possibility for what `s2 = s1` might do, if Rust chose to copy heap data as well. + Earlier, we said that when a variable goes out of scope, Rust will automatically call the `drop` function and clean up the heap memory for that @@ -344,7 +350,9 @@ into `s2`. So what actually happens looks like Figure 4-4. s1 moved to s2 + Figure 4-4: Representation in memory after `s1` has been invalidated + That solves our problem! With only `s2` valid, when it goes out of scope, it alone will free the memory, and we’re done. @@ -565,7 +573,9 @@ taking ownership of it. Figure 4-5 shows a diagram of this. &String s pointing at String s1 + Figure 4-5: `&String s` pointing at `String s1` + Let’s take a closer look at the function call here: @@ -970,7 +980,9 @@ Figure 4-6 shows this in a diagram: world containing a pointer to the 6th byte of String s and a length 5 + Figure 4-6: String slice referring to part of a `String` + With Rust’s `..` range syntax, if you want to start at the first index (zero), you can drop the value before the `..`. In other words, these are equal: diff --git a/src/ch04-01-ownership.md b/src/ch04-01-ownership.md index ce00c6f..1d113e1 100644 --- a/src/ch04-01-ownership.md +++ b/src/ch04-01-ownership.md @@ -262,8 +262,10 @@ is the memory that holds the contents, and this is on the heap. String in memory + Figure 4-1: Representation in memory of a `String` holding the value `"hello"` bound to `s1` + The length is how much memory, in bytes, the contents of the `String` is currently using. The capacity is the total amount of memory, in bytes, that the @@ -278,8 +280,10 @@ words, it looks like figure 4-2. s1 and s2 pointing to the same value + Figure 4-2: Representation in memory of the variable `s2` that has a copy of `s1`’s pointer, length and capacity + And *not* Figure 4-3, which is what memory would look like if Rust instead copied the heap data as well. If Rust did this, the operation `s2 = s1` could @@ -287,8 +291,10 @@ potentially be very expensive if the data on the heap was large. s1 and s2 to two places + Figure 4-3: Another possibility for what `s2 = s1` might do, if Rust chose to copy heap data as well. + Earlier, we said that when a variable goes out of scope, Rust will automatically call the `drop` function and clean up the heap memory for that variable. But @@ -332,7 +338,9 @@ into `s2`. So what actually happens looks like Figure 4-4. s1 moved to s2 + Figure 4-4: Representation in memory after `s1` has been invalidated + That solves our problem! With only `s2` valid, when it goes out of scope, it alone will free the memory, and we’re done. diff --git a/src/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md index 60312c3..598b7b7 100644 --- a/src/ch04-02-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -35,7 +35,9 @@ without taking ownership of it. Figure 4-5 shows a diagram of this. &String s pointing at String s1 + Figure 4-5: `&String s` pointing at `String s1` + Let’s take a closer look at the function call here: diff --git a/src/ch04-03-slices.md b/src/ch04-03-slices.md index ec4dae4..0a10a08 100644 --- a/src/ch04-03-slices.md +++ b/src/ch04-03-slices.md @@ -152,7 +152,9 @@ Figure 4-6 shows this in a diagram: world containing a pointer to the 6th byte of String s and a length 5 + Figure 4-6: String slice referring to part of a `String` + With Rust’s `..` range syntax, if you want to start at the first index (zero), you can drop the value before the `..`. In other words, these are equal: