diff --git a/src/ch04-01-ownership.md b/src/ch04-01-ownership.md index de9d33622..9a6f95f7f 100644 --- a/src/ch04-01-ownership.md +++ b/src/ch04-01-ownership.md @@ -167,7 +167,7 @@ You might say “copy the `String`!” This is both correct and incorrect at the same time. It does a _shallow_ copy of the `String`. What’s that mean? Well, let’s take a look at what `String` looks like under the covers: -string +String in memory A `String` is made up of three parts: a pointer to the memory that holds the contents of the string, a length, and a capacity. The length is how much memory @@ -180,11 +180,11 @@ When we assign `s1` to `s2`, the `String` itself is copied, meaning we copy the pointer, the length, and the capacity. We do not copy the data that the `String`'s pointer refers to. In other words, it looks like this: -s1 and s2 +s1 and s2 pointing to the same value _Not_ this: -s1 and s2 to two places +s1 and s2 to two places There’s a problem here. Both data pointers are pointing to the same place. Why is this a problem? Well, when `s2` goes out of scope, it will free the memory @@ -240,7 +240,7 @@ also invalidates the first binding, instead of calling this a shallow copy, it's called a _move_. Here we would read this by saying that `s1` was _moved_ into `s2`. So what actually happens looks like this: -s1 and s2 to the same place +s1 moved to s2 That solves our problem! With only `s2` valid, when it goes out of scope, it alone will free the memory, and we’re done. @@ -279,7 +279,7 @@ println!("{}", s1); This will work just fine. Remember our diagram from before? In this case, it _is_ doing this: -s1 and s2 to two places +s1 and s2 to two places When you see a call to `clone()`, you know that some arbitrary code is being executed, and that code may be expensive. It’s a visual indicator that something diff --git a/src/img/trpl04-01.png b/src/img/trpl04-01.png deleted file mode 100644 index 5976fe4a2..000000000 Binary files a/src/img/trpl04-01.png and /dev/null differ diff --git a/src/img/trpl04-01.svg b/src/img/trpl04-01.svg new file mode 100644 index 000000000..7f5ee8d53 --- /dev/null +++ b/src/img/trpl04-01.svg @@ -0,0 +1,65 @@ + + + + + + +%3 + + +table0 + +s1 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table1 + +index + +value + +0 + +h + +1 + +e + +2 + +l + +3 + +l + +4 + +o + + +table0:pointer:c->table1:pointee + + + + + diff --git a/src/img/trpl04-02.png b/src/img/trpl04-02.png deleted file mode 100644 index a8cd782fa..000000000 Binary files a/src/img/trpl04-02.png and /dev/null differ diff --git a/src/img/trpl04-02.svg b/src/img/trpl04-02.svg new file mode 100644 index 000000000..7d3a29a82 --- /dev/null +++ b/src/img/trpl04-02.svg @@ -0,0 +1,90 @@ + + + + + + +%3 + + +table0 + +s1 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table1 + +index + +value + +0 + +h + +1 + +e + +2 + +l + +3 + +l + +4 + +o + + +table0:pointer:c->table1:pointee + + + + +table3 + +s2 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table3:pointer:c->table1:pointee + + + + + diff --git a/src/img/trpl04-03.png b/src/img/trpl04-03.png deleted file mode 100644 index 595f1d6e1..000000000 Binary files a/src/img/trpl04-03.png and /dev/null differ diff --git a/src/img/trpl04-03.svg b/src/img/trpl04-03.svg new file mode 100644 index 000000000..e2403fc02 --- /dev/null +++ b/src/img/trpl04-03.svg @@ -0,0 +1,117 @@ + + + + + + +%3 + + +table0 + +s1 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table1 + +index + +value + +0 + +h + +1 + +e + +2 + +l + +3 + +l + +4 + +o + + +table0:pointer:c->table1:pointee + + + + +table3 + +s2 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table4 + +index + +value + +0 + +h + +1 + +e + +2 + +l + +3 + +l + +4 + +o + + +table3:pointer:c->table4:pointee + + + + + diff --git a/src/img/trpl04-04.png b/src/img/trpl04-04.png deleted file mode 100644 index 895576589..000000000 Binary files a/src/img/trpl04-04.png and /dev/null differ diff --git a/src/img/trpl04-04.svg b/src/img/trpl04-04.svg new file mode 100644 index 000000000..1a17b2766 --- /dev/null +++ b/src/img/trpl04-04.svg @@ -0,0 +1,91 @@ + + + + + + +%3 + + +table0 + + +s1 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table1 + +index + +value + +0 + +h + +1 + +e + +2 + +l + +3 + +l + +4 + +o + + +table0:pointer:c->table1:pointee + + + + +table3 + +s2 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table3:pointer:c->table1:pointee + + + + +