From aceb7d00e559fd2775ec73f043b12542ed7ff616 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 7 Nov 2016 18:15:08 -0500 Subject: [PATCH] Transferring eddyb's comments to markdown for ease of discussion --- nostarch/chapter04.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/nostarch/chapter04.md b/nostarch/chapter04.md index f3eedf245..9dfb253cc 100644 --- a/nostarch/chapter04.md +++ b/nostarch/chapter04.md @@ -45,7 +45,7 @@ explanation. Both the stack and the heap are parts of memory that is available to your code to use at runtime, but they are structured in different ways. The stack stores values in the order it gets them and removes the values in the opposite order. -This is referred to as *last in, first out*. Think of a stack of plates: when +This is referred to as *last in, first out*. Think of a stack of plates: when you add more plates, you put them on top of the pile, and when you need a plate, you take one off the top. Adding or removing plates from the middle or bottom wouldn’t work as well! Adding data is called *pushing onto the stack* @@ -320,7 +320,7 @@ automatically call the `drop` function and clean up the heap memory for that variable. But in figure 4-2, we see both data pointers pointing to the same location. This is a problem: when `s2` and `s1` go out of scope, they will both try to free the same memory. This is known as a *double free* error and is one -of the memory safety bugs we mentioned before. Freeing memory twice can lead to +of the memory safety bugs we mentioned before. Freeing memory twice can lead to memory corruption, which can potentially lead to security vulnerabilities. In order to ensure memory safety, there’s one more detail to what happens in @@ -365,7 +365,7 @@ That solves our problem! With only `s2` valid, when it goes out of scope, it alone will free the memory, and we’re done. Furthermore, there’s a design choice that’s implied by this: Rust will never -automatically create “deep” copies of your data. Therefore, any *automatic* +automatically create “deep” copies of your data. Therefore, any *automatic* copying can be assumed to be inexpensive. #### Ways Variables and Data Interact: Clone @@ -417,7 +417,7 @@ Rust has a special annotation called the `Copy` trait that we can place on types like these (we’ll talk more about traits in Chapter 10). If a type has the `Copy` trait, an older variable is still usable after assignment. Rust will not let us annotate a type with the `Copy` trait if the type, or any of its -parts, has implemented `drop`. If the type needs something special to happen +parts, has implemented `drop`. If the type needs something special to happen when the value goes out of scope and we add the `Copy` annotation to that type, we will get a compile-time error. @@ -991,6 +991,8 @@ 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`