diff --git a/dot/foo1.dot b/dot/trpl04-01.dot similarity index 100% rename from dot/foo1.dot rename to dot/trpl04-01.dot diff --git a/dot/foo2.dot b/dot/trpl04-02.dot similarity index 100% rename from dot/foo2.dot rename to dot/trpl04-02.dot diff --git a/dot/foo4.dot b/dot/trpl04-03.dot similarity index 100% rename from dot/foo4.dot rename to dot/trpl04-03.dot diff --git a/dot/foo3.dot b/dot/trpl04-04.dot similarity index 100% rename from dot/foo3.dot rename to dot/trpl04-04.dot diff --git a/dot/trpl04-05.dot b/dot/trpl04-05.dot new file mode 100644 index 000000000..55aa221fd --- /dev/null +++ b/dot/trpl04-05.dot @@ -0,0 +1,31 @@ +digraph { + rankdir=LR; + dpi=300.0; + node [shape="plaintext"]; + + table0[label=< + + + +
s
namevalue
ptr
>]; + table1[label=< + + + + + +
s1
namevalue
ptr
len5
capacity5
>]; + table2[label=< + + + + + + +
indexvalue
0h
1e
2l
3l
4o
>]; + + edge[tailclip="false"]; + table1:pointer:c -> table2:pointee; + table0:borrower:c -> table1:borrowee; +} + diff --git a/dot/trpl04-06.dot b/dot/trpl04-06.dot new file mode 100644 index 000000000..6a92e9cc2 --- /dev/null +++ b/dot/trpl04-06.dot @@ -0,0 +1,40 @@ +digraph { + rankdir=LR; + dpi=300.0; + node [shape="plaintext"]; + + table0[label=< + + + + +
world
namevalue
ptr
len5
>]; + + table3[label=< + + + + + +
s
namevalue
ptr
len5
capacity5
>]; + table4[label=< + + + + + + + + + + + + +
indexvalue
0h
1e
2l
3l
4o
5
6w
7o
8r
9l
10d
>]; + + + edge[tailclip="false"]; + table0:pointer2:c -> table4:pointee2; + table3:pointer:c -> table4:pointee; +} + diff --git a/src/ch04-01-ownership.md b/src/ch04-01-ownership.md index 020ca2b22..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/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md index 2861f6e4a..47ad3078e 100644 --- a/src/ch04-02-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -53,7 +53,7 @@ function return value is gone. Next, note that we pass `&s1` into These `&`s are called *references*, and they allow you to refer to some value without taking ownership of it. Here’s a diagram: -DIAGRAM GOES HERE of a &String pointing at a String, with (ptr, len, capacity) +&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 40512d6e3..b1395b178 100644 --- a/src/ch04-03-slices.md +++ b/src/ch04-03-slices.md @@ -141,7 +141,7 @@ contains a pointer to the 6th byte of `s` and a length value of 5. In other words, it looks like this: -DIAGRAM GOES HERE of s, hello, and world +world containing a pointer to the 6th byte of String s and a length 5 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/img/foo1.png b/src/img/foo1.png deleted file mode 100644 index 5976fe4a2..000000000 Binary files a/src/img/foo1.png and /dev/null differ diff --git a/src/img/foo2.png b/src/img/foo2.png deleted file mode 100644 index a8cd782fa..000000000 Binary files a/src/img/foo2.png and /dev/null differ diff --git a/src/img/foo3.png b/src/img/foo3.png deleted file mode 100644 index 895576589..000000000 Binary files a/src/img/foo3.png and /dev/null differ diff --git a/src/img/foo4.png b/src/img/foo4.png deleted file mode 100644 index 595f1d6e1..000000000 Binary files a/src/img/foo4.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.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.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.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 + + + + + diff --git a/src/img/trpl04-05.svg b/src/img/trpl04-05.svg new file mode 100644 index 000000000..33e5b49fa --- /dev/null +++ b/src/img/trpl04-05.svg @@ -0,0 +1,82 @@ + + + + + + +%3 + + +table0 + +s + +name + +value + +ptr + + + +table1 + +s1 + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table0:borrower:c->table1:borrowee + + + + +table2 + +index + +value + +0 + +h + +1 + +e + +2 + +l + +3 + +l + +4 + +o + + +table1:pointer:c->table2:pointee + + + + + diff --git a/src/img/trpl04-06.svg b/src/img/trpl04-06.svg new file mode 100644 index 000000000..16510b1e6 --- /dev/null +++ b/src/img/trpl04-06.svg @@ -0,0 +1,110 @@ + + + + + + +%3 + + +table0 + +world + +name + +value + +ptr + + +len + +5 + + +table4 + +index + +value + +0 + +h + +1 + +e + +2 + +l + +3 + +l + +4 + +o + +5 + + + +6 + +w + +7 + +o + +8 + +r + +9 + +l + +10 + +d + + +table0:pointer2:c->table4:pointee2 + + + + +table3 + +s + +name + +value + +ptr + + +len + +5 + +capacity + +5 + + +table3:pointer:c->table4:pointee + + + + +