mirror of
https://github.com/rust-lang/book.git
synced 2026-12-01 16:36:11 -06:00
Add more figures
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
digraph {
|
||||
rankdir=LR;
|
||||
dpi=300.0;
|
||||
node [shape="plaintext"];
|
||||
|
||||
table4[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD SIDES="B">b</TD><TD SIDES="B" PORT="ptr4"></TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
table5[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD PORT="pte4">3</TD><TD PORT="ptr5"> </TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
|
||||
table0[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD SIDES="B">a</TD><TD SIDES="B" PORT="ptr0"></TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
table1[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD PORT="pte0">5</TD><TD PORT="ptr1"> </TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
table2[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD PORT="pte1">10</TD><TD PORT="ptr2"> </TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
table3[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD PORT="pte2">Nil</TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
|
||||
table6[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD SIDES="B">c</TD><TD SIDES="B" PORT="ptr6"></TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
table7[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
|
||||
<TR><TD PORT="pte6">4</TD><TD PORT="ptr7"> </TD></TR>
|
||||
</TABLE>>];
|
||||
|
||||
|
||||
edge[tailclip="false"];
|
||||
table0:ptr0:c -> table1:pte0;
|
||||
table1:ptr1:c -> table2:pte1;
|
||||
table2:ptr2:c -> table3:pte2;
|
||||
table4:ptr4:c -> table5:pte4;
|
||||
table5:ptr5:c -> table1:pte0;
|
||||
table6:ptr6:c -> table7:pte6;
|
||||
table7:ptr7:c -> table1:pte0;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ to be able to use smart pointers in the same places that we can use regular
|
||||
references. To enable that, we can override the behavior of the `*` operator by
|
||||
implementing the `Deref` trait.
|
||||
|
||||
Listing 15-5 has an example of overriding `*` using `Deref` on a struct we've
|
||||
Listing 15-7 has an example of overriding `*` using `Deref` on a struct we've
|
||||
defined to hold mp3 data and metadata. `Mp3` is, in a sense, a smart pointer:
|
||||
it owns the `Vec<u8>` data containing the audio. In addition, it holds some
|
||||
optional metadata, in this case the artist and title of the song in the audio
|
||||
@@ -79,7 +79,7 @@ fn main() {
|
||||
|
||||
<figcaption>
|
||||
|
||||
Listing 15-5: An implementation of the `Deref` trait on a struct that holds mp3
|
||||
Listing 15-7: An implementation of the `Deref` trait on a struct that holds mp3
|
||||
file data and metadata
|
||||
|
||||
</figcaption>
|
||||
|
||||
+5
-5
@@ -21,7 +21,7 @@ The way we specify code should be run when a value goes out of scope is by
|
||||
implementing the `Drop` trait. The `Drop` trait requires us to implement one
|
||||
method named `drop` that takes a mutable reference to `self`.
|
||||
|
||||
Listing 15-6 shows a `CustomSmartPointer` struct that doesn't actually do
|
||||
Listing 15-8 shows a `CustomSmartPointer` struct that doesn't actually do
|
||||
anything, but we're printing out `CustomSmartPointer created.` right after we
|
||||
create an instance of the struct and `Dropping CustomSmartPointer!` when the
|
||||
instance goes out of scope so that we can see when each piece of code gets run.
|
||||
@@ -51,7 +51,7 @@ fn main() {
|
||||
|
||||
<figcaption>
|
||||
|
||||
Listing 15-6: A `CustomSmartPointer` struct that implements the `Drop` trait,
|
||||
Listing 15-8: A `CustomSmartPointer` struct that implements the `Drop` trait,
|
||||
where we could put code that would clean up after the `CustomSmartPointer`.
|
||||
|
||||
</figcaption>
|
||||
@@ -82,7 +82,7 @@ trait is that it's taken care of automatically for us. We'll see an example of
|
||||
a case when we'll need to drop a value earlier than when it goes out of scope
|
||||
in Chapter 16 when we're talking about concurrency. For now, let's just see
|
||||
that it's possible, and `std::mem::drop` is in the prelude so we can just call
|
||||
`drop` as shown in Listing 15-7:
|
||||
`drop` as shown in Listing 15-9:
|
||||
|
||||
<figure>
|
||||
|
||||
@@ -97,7 +97,7 @@ fn main() {
|
||||
|
||||
<figcaption>
|
||||
|
||||
Listing 15-7: Calling `std::mem::drop` to explicitly drop a value before it
|
||||
Listing 15-9: Calling `std::mem::drop` to explicitly drop a value before it
|
||||
goes out of scope
|
||||
|
||||
</figcaption>
|
||||
@@ -114,7 +114,7 @@ Wait for it...
|
||||
```
|
||||
|
||||
Note that we aren't allowed to call the `drop` method that we defined directly:
|
||||
if we replaced `drop(c)` in Listing 15-7 with `c.drop()`, we'll get a compiler
|
||||
if we replaced `drop(c)` in Listing 15-9 with `c.drop()`, we'll get a compiler
|
||||
error that says `explicit destructor calls not allowed`. We're not allowed to
|
||||
call `Drop::drop` directly because when Rust inserts its call to `Drop::drop`
|
||||
automatically when the value goes out of scope, then the value would get
|
||||
|
||||
+18
-13
@@ -29,23 +29,28 @@ you'll get a compile-time error.
|
||||
|
||||
### Using `Rc<T>` to Share Data
|
||||
|
||||
Let's return to our cons list example from Listing 15-4. In Listing 15-8, we're
|
||||
Let's return to our cons list example from Listing 15-5. In Listing 15-11, we're
|
||||
going to try to use `List` as we defined it using `Box<T>`. First we'll create
|
||||
one list instance that contains 5 and then 10. Next, we want to create two more
|
||||
lists: one that starts with 3 and continues on to our first list containing 5
|
||||
and 10, then another list that starts with 4 and *also* continues on to our
|
||||
first list containing 5 and 10. In other words, we want two lists that both
|
||||
share ownership of the third list, which conceptually will be something like
|
||||
this:
|
||||
Figure 15-10:
|
||||
|
||||
```text
|
||||
b -> 3 ---v
|
||||
a ------> 5 -> 10 -> Nil
|
||||
c -> 4 ---^
|
||||
```
|
||||
<figure>
|
||||
|
||||
<img alt="Two lists that share ownership of a third list" src="img/trpl15-03.svg" class="center" />
|
||||
|
||||
<figcaption>
|
||||
|
||||
Figure 15-10: Two lists, `b` and `c`, sharing ownership of a third list, `a`
|
||||
|
||||
</figcaption>
|
||||
</figure>
|
||||
|
||||
Trying to implement this using our definition of `List` with `Box<T>` won't
|
||||
work:
|
||||
work, as shown in Listing 15-11:
|
||||
|
||||
<figure>
|
||||
<span class="filename">Filename: src/main.rs</span>
|
||||
@@ -69,7 +74,7 @@ fn main() {
|
||||
|
||||
<figcaption>
|
||||
|
||||
Listing 15-8: Having two lists using `Box<T>` that try to share ownership of a
|
||||
Listing 15-11: Having two lists using `Box<T>` that try to share ownership of a
|
||||
third list won't work
|
||||
|
||||
</figcaption>
|
||||
@@ -100,7 +105,7 @@ a list such that every element lives at least as long as the list itself.
|
||||
Otherwise, the borrow checker won't even let us compile the code.
|
||||
|
||||
Instead, we can change our definition of `List` to use `Rc<T>` instead of
|
||||
`Box<T>` as shown here in Listing 15-9:
|
||||
`Box<T>` as shown here in Listing 15-12:
|
||||
|
||||
<figure>
|
||||
<span class="filename">Filename: src/main.rs</span>
|
||||
@@ -123,7 +128,7 @@ fn main() {
|
||||
|
||||
<figcaption>
|
||||
|
||||
Listing 15-9: A definition of `List` that uses `Rc<T>`
|
||||
Listing 15-12: A definition of `List` that uses `Rc<T>`
|
||||
|
||||
</figcaption>
|
||||
</figure>
|
||||
@@ -137,7 +142,7 @@ prelude. In `main`, we create the list holding 5 and 10 and store it in a new
|
||||
We've seen the `clone` method previously, where we used it for making a
|
||||
complete copy of some data. With `Rc<T>`, though, it doesn't make a full copy.
|
||||
`Rc<T>` holds a *reference count*, that is, a count of how many clones exist.
|
||||
Let's change `main` as shown in Listing 15-10 to have an inner scope around
|
||||
Let's change `main` as shown in Listing 15-13 to have an inner scope around
|
||||
where we create `c`, and to print out the results of the `Rc::strong_count`
|
||||
associated function at various points. `Rc::strong_count` returns the reference
|
||||
count of the `Rc` value we pass to it, and we'll talk about why this function
|
||||
@@ -171,7 +176,7 @@ fn main() {
|
||||
|
||||
<figcaption>
|
||||
|
||||
Listing 15-10: Printing out the reference count
|
||||
Listing 15-13: Printing out the reference count
|
||||
|
||||
</figcaption>
|
||||
</figure>
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
||||
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
|
||||
-->
|
||||
<!-- Title: %3 Pages: 1 -->
|
||||
<svg width="750pt"
|
||||
viewBox="0.00 0.00 2500 700" xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<g id="graph0" class="graph" transform="scale(4.16667 4.16667) rotate(0) translate(4 148)">
|
||||
<title>%3</title>
|
||||
<polygon fill="white" stroke="none" points="-4,4 -4,-148 418,-148 418,4 -4,4"/>
|
||||
<!-- table4 -->
|
||||
<g id="node1" class="node"><title>table4</title>
|
||||
<text text-anchor="start" x="21" y="-121.8" font-family="Times,serif" font-size="14.00">b</text>
|
||||
</g>
|
||||
<!-- table5 -->
|
||||
<g id="node2" class="node"><title>table5</title>
|
||||
<polygon fill="none" stroke="black" points="104,-116 104,-136 117,-136 117,-116 104,-116"/>
|
||||
<text text-anchor="start" x="107" y="-121.8" font-family="Times,serif" font-size="14.00">3</text>
|
||||
<polygon fill="none" stroke="black" points="117,-116 117,-136 130,-136 130,-116 117,-116"/>
|
||||
<text text-anchor="start" x="120" y="-121.8" font-family="Times,serif" font-size="14.00">  </text>
|
||||
</g>
|
||||
<!-- table4->table5 -->
|
||||
<g id="edge4" class="edge"><title>table4:ptr4:c->table5:pte4</title>
|
||||
<path fill="none" stroke="black" d="M35,-126C35,-126 64.9658,-126 92.8715,-126"/>
|
||||
<polygon fill="black" stroke="black" points="93,-129.5 103,-126 93,-122.5 93,-129.5"/>
|
||||
</g>
|
||||
<!-- table1 -->
|
||||
<g id="node4" class="node"><title>table1</title>
|
||||
<polygon fill="none" stroke="black" points="194,-62 194,-82 207,-82 207,-62 194,-62"/>
|
||||
<text text-anchor="start" x="197" y="-67.8" font-family="Times,serif" font-size="14.00">5</text>
|
||||
<polygon fill="none" stroke="black" points="207,-62 207,-82 220,-82 220,-62 207,-62"/>
|
||||
<text text-anchor="start" x="210" y="-67.8" font-family="Times,serif" font-size="14.00">  </text>
|
||||
</g>
|
||||
<!-- table5->table1 -->
|
||||
<g id="edge5" class="edge"><title>table5:ptr5:c->table1:pte0</title>
|
||||
<path fill="none" stroke="black" d="M125,-126C125,-126 149.361,-81.2672 182.835,-73.2293"/>
|
||||
<polygon fill="black" stroke="black" points="183.493,-76.6754 193,-72 182.652,-69.726 183.493,-76.6754"/>
|
||||
</g>
|
||||
<!-- table0 -->
|
||||
<g id="node3" class="node"><title>table0</title>
|
||||
<text text-anchor="start" x="110.893" y="-67.8" font-family="Times,serif" font-size="14.00">a</text>
|
||||
</g>
|
||||
<!-- table0->table1 -->
|
||||
<g id="edge1" class="edge"><title>table0:ptr0:c->table1:pte0</title>
|
||||
<path fill="none" stroke="black" d="M124,-72C124,-72 154.406,-72 182.723,-72"/>
|
||||
<polygon fill="black" stroke="black" points="183,-75.5001 193,-72 183,-68.5001 183,-75.5001"/>
|
||||
</g>
|
||||
<!-- table2 -->
|
||||
<g id="node5" class="node"><title>table2</title>
|
||||
<polygon fill="none" stroke="black" points="281,-62 281,-82 301,-82 301,-62 281,-62"/>
|
||||
<text text-anchor="start" x="284" y="-67.8" font-family="Times,serif" font-size="14.00">10</text>
|
||||
<polygon fill="none" stroke="black" points="301,-62 301,-82 314,-82 314,-62 301,-62"/>
|
||||
<text text-anchor="start" x="304" y="-67.8" font-family="Times,serif" font-size="14.00">  </text>
|
||||
</g>
|
||||
<!-- table1->table2 -->
|
||||
<g id="edge2" class="edge"><title>table1:ptr1:c->table2:pte1</title>
|
||||
<path fill="none" stroke="black" d="M215,-72C215,-72 243.393,-72 269.967,-72"/>
|
||||
<polygon fill="black" stroke="black" points="270,-75.5001 280,-72 270,-68.5001 270,-75.5001"/>
|
||||
</g>
|
||||
<!-- table3 -->
|
||||
<g id="node6" class="node"><title>table3</title>
|
||||
<polygon fill="none" stroke="black" points="376,-62 376,-82 399,-82 399,-62 376,-62"/>
|
||||
<text text-anchor="start" x="378.555" y="-67.8" font-family="Times,serif" font-size="14.00">Nil</text>
|
||||
</g>
|
||||
<!-- table2->table3 -->
|
||||
<g id="edge3" class="edge"><title>table2:ptr2:c->table3:pte2</title>
|
||||
<path fill="none" stroke="black" d="M309,-72C309,-72 342.423,-72 365.512,-72"/>
|
||||
<polygon fill="black" stroke="black" points="365.825,-75.5001 375.825,-72 365.824,-68.5001 365.825,-75.5001"/>
|
||||
</g>
|
||||
<!-- table6 -->
|
||||
<g id="node7" class="node"><title>table6</title>
|
||||
<text text-anchor="start" x="20.8931" y="-13.8" font-family="Times,serif" font-size="14.00">c</text>
|
||||
</g>
|
||||
<!-- table7 -->
|
||||
<g id="node8" class="node"><title>table7</title>
|
||||
<polygon fill="none" stroke="black" points="104,-8 104,-28 117,-28 117,-8 104,-8"/>
|
||||
<text text-anchor="start" x="107" y="-13.8" font-family="Times,serif" font-size="14.00">4</text>
|
||||
<polygon fill="none" stroke="black" points="117,-8 117,-28 130,-28 130,-8 117,-8"/>
|
||||
<text text-anchor="start" x="120" y="-13.8" font-family="Times,serif" font-size="14.00">  </text>
|
||||
</g>
|
||||
<!-- table6->table7 -->
|
||||
<g id="edge6" class="edge"><title>table6:ptr6:c->table7:pte6</title>
|
||||
<path fill="none" stroke="black" d="M34,-18C34,-18 64.4065,-18 92.7225,-18"/>
|
||||
<polygon fill="black" stroke="black" points="93,-21.5001 103,-18 93,-14.5001 93,-21.5001"/>
|
||||
</g>
|
||||
<!-- table7->table1 -->
|
||||
<g id="edge7" class="edge"><title>table7:ptr7:c->table1:pte0</title>
|
||||
<path fill="none" stroke="black" d="M125,-18C125,-18 149.361,-62.7328 182.835,-70.7707"/>
|
||||
<polygon fill="black" stroke="black" points="182.652,-74.274 193,-72 183.493,-67.3246 182.652,-74.274"/>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.1 KiB |
Reference in New Issue
Block a user