Skip to content

Commit

Permalink
Redo the listing numbers again because there are Figures
Browse files Browse the repository at this point in the history
Luckily I named the figure image files correctly...
  • Loading branch information
carols10cents committed Jan 30, 2018
1 parent 3e28894 commit 1d532d5
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 156 deletions.
156 changes: 78 additions & 78 deletions second-edition/nostarch/chapter15.md

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions second-edition/src/ch15-01-box.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 198,11 @@ of type `List`. Therefore, `Cons` needs an amount of space equal to the size of
an `i32` plus the size of a `List`. To figure out how much memory the `List`
type needs, the compiler looks at the variants, starting with the `Cons`
variant. The `Cons` variant holds a value of type `i32` and a value of type
`List`, and this continues infinitely, as shown in Figure 15-5.
`List`, and this continues infinitely, as shown in Figure 15-1.

<img alt="An infinite Cons list" src="img/trpl15-01.svg" class="center" style="width: 50%;" />

<span class="caption">Figure 15-5: An infinite `List` consisting of infinite
<span class="caption">Figure 15-1: An infinite `List` consisting of infinite
`Cons` variants</span>

### Using `Box<T>` to Get a Recursive Type with a Known Size
Expand Down Expand Up @@ -232,7 232,7 @@ is now more like the items being next to one another rather than inside one
another.

We can change the definition of the `List` enum from Listing 15-2 and the usage
of the `List` from Listing 15-3 to the code in Listing 15-6, which will compile:
of the `List` from Listing 15-3 to the code in Listing 15-5, which will compile:

<span class="filename">Filename: src/main.rs</span>

Expand All @@ -252,20 252,20 @@ fn main() {
}
```

<span class="caption">Listing 15-6: Definition of `List` that uses `Box<T>` in
<span class="caption">Listing 15-5: Definition of `List` that uses `Box<T>` in
order to have a known size</span>

The `Cons` variant will need the size of an `i32` plus the space to store the
box’s pointer data. The `Nil` variant stores no values, so it needs less space
than the `Cons` variant. We now know that any `List` value will take up the
size of an `i32` plus the size of a box’s pointer data. By using a box, we’ve
broken the infinite, recursive chain so the compiler is able to figure out the
size it needs to store a `List` value. Figure 15-7 shows what the `Cons`
size it needs to store a `List` value. Figure 15-2 shows what the `Cons`
variant looks like now:

<img alt="A finite Cons list" src="img/trpl15-02.svg" class="center" />

<span class="caption">Figure 15-7: A `List` that is not infinitely sized since
<span class="caption">Figure 15-2: A `List` that is not infinitely sized since
`Cons` holds a `Box`</span>

Boxes only provide the indirection and heap allocation; they don’t have any
Expand Down
50 changes: 25 additions & 25 deletions second-edition/src/ch15-02-deref.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 16,7 @@ references or smart pointers.
### Following the Pointer to the Value with `*`

A regular reference is a type of pointer, and one way to think of a pointer is
that it’s an arrow to a value stored somewhere else. In Listing 15-8, let’s
that it’s an arrow to a value stored somewhere else. In Listing 15-6, let’s
create a reference to an `i32` value then use the dereference operator to
follow the reference to the data:

Expand All @@ -32,7 32,7 @@ fn main() {
}
```

<span class="caption">Listing 15-8: Using the dereference operator to follow a
<span class="caption">Listing 15-6: Using the dereference operator to follow a
reference to an `i32` value</span>

The variable `x` holds an `i32` value, `5`. We set `y` equal to a reference to
Expand Down Expand Up @@ -63,9 63,9 @@ pointing to.

### Using `Box<T>` Like a Reference

We can rewrite the code in Listing 15-8 to use a `Box<T>` instead of a
We can rewrite the code in Listing 15-6 to use a `Box<T>` instead of a
reference, and the de-reference operator will work the same way as shown in
Listing 15-9:
Listing 15-7:

<span class="filename">Filename: src/main.rs</span>

Expand All @@ -79,10 79,10 @@ fn main() {
}
```

<span class="caption">Listing 15-9: Using the dereference operator on a
<span class="caption">Listing 15-7: Using the dereference operator on a
`Box<i32>`</span>

The only part of Listing 15-8 that we changed was to set `y` to be an instance
The only part of Listing 15-6 that we changed was to set `y` to be an instance
of a box pointing to the value in `x` rather than a reference pointing to the
value of `x`. In the last assertion, we can use the dereference operator to
follow the box’s pointer in the same way that we did when `y` was a reference.
Expand All @@ -97,7 97,7 @@ behave like references by default. Then we’ll learn about how to add the
ability to use the dereference operator.

`Box<T>` is ultimately defined as a tuple struct with one element, so Listing
15-10 defines a `MyBox<T>` type in the same way. We’ll also define a `new`
15-8 defines a `MyBox<T>` type in the same way. We’ll also define a `new`
function to match the `new` function defined on `Box<T>`:

<span class="filename">Filename: src/main.rs</span>
Expand All @@ -112,16 112,16 @@ impl<T> MyBox<T> {
}
```

<span class="caption">Listing 15-10: Defining a `MyBox<T>` type</span>
<span class="caption">Listing 15-8: Defining a `MyBox<T>` type</span>

We define a struct named `MyBox` and declare a generic parameter `T`, since we
want our type to be able to hold values of any type. `MyBox` is a tuple struct
with one element of type `T`. The `MyBox::new` function takes one parameter of
type `T` and returns a `MyBox` instance that holds the value passed in.

Let’s try adding the code from Listing 15-9 to the code in Listing 15-10 and
Let’s try adding the code from Listing 15-7 to the code in Listing 15-8 and
changing `main` to use the `MyBox<T>` type we’ve defined instead of `Box<T>`.
The code in Listing 15-11 won’t compile because Rust doesn’t know how to
The code in Listing 15-9 won’t compile because Rust doesn’t know how to
dereference `MyBox`:

<span class="filename">Filename: src/main.rs</span>
Expand All @@ -136,7 136,7 @@ fn main() {
}
```

<span class="caption">Listing 15-11: Attempting to use `MyBox<T>` in the same
<span class="caption">Listing 15-9: Attempting to use `MyBox<T>` in the same
way we were able to use references and `Box<T>`</span>

The compilation error we get is:
Expand All @@ -159,7 159,7 @@ As we discussed in Chapter 10, in order to implement a trait, we need to
provide implementations for the trait’s required methods. The `Deref` trait,
provided by the standard library, requires implementing one method named
`deref` that borrows `self` and returns a reference to the inner data. Listing
15-12 contains an implementation of `Deref` to add to the definition of `MyBox`:
15-10 contains an implementation of `Deref` to add to the definition of `MyBox`:

<span class="filename">Filename: src/main.rs</span>

Expand All @@ -176,7 176,7 @@ impl<T> Deref for MyBox<T> {
}
```

<span class="caption">Listing 15-12: Implementing `Deref` on `MyBox<T>`</span>
<span class="caption">Listing 15-10: Implementing `Deref` on `MyBox<T>`</span>

The `type Target = T;` syntax defines an associated type for this trait to use.
Associated types are a slightly different way of declaring a generic parameter
Expand All @@ -185,15 185,15 @@ detail in Chapter 19.

We filled in the body of the `deref` method with `&self.0` so that `deref`
returns a reference to the value we want to access with the `*` operator. The
`main` function from Listing 15-11 that calls `*` on the `MyBox<T>` value now
`main` function from Listing 15-9 that calls `*` on the `MyBox<T>` value now
compiles and the assertions pass!

Without the `Deref` trait, the compiler can only dereference `&` references.
The `Deref` trait’s `deref` method gives the compiler the ability to take a
value of any type that implements `Deref` and call the `deref` method in order
to get a `&` reference that it knows how to dereference.

When we typed `*y` in Listing 15-11, what Rust actually ran behind the scenes
When we typed `*y` in Listing 15-9, what Rust actually ran behind the scenes
was this code:

```rust,ignore
Expand All @@ -216,7 216,7 @@ most cases where we use the dereference operator.
Note that replacing `*` with a call to the `deref` method and then a call to
`*` happens once, each time we type a `*` in our code. The substitution of `*`
does not recurse infinitely. That’s how we end up with data of type `i32`,
which matches the `5` in the `assert_eq!` in Listing 15-11.
which matches the `5` in the `assert_eq!` in Listing 15-9.

### Implicit Deref Coercions with Functions and Methods

Expand All @@ -235,8 235,8 @@ with `&` and `*`. This feature also lets us write more code that can work for
either references or smart pointers.

To illustrate deref coercion in action, let’s use the `MyBox<T>` type we
defined in Listing 15-10 as well as the implementation of `Deref` that we added
in Listing 15-12. Listing 15-13 shows the definition of a function that has a
defined in Listing 15-8 as well as the implementation of `Deref` that we added
in Listing 15-10. Listing 15-11 shows the definition of a function that has a
string slice parameter:

<span class="filename">Filename: src/main.rs</span>
Expand All @@ -247,13 247,13 @@ fn hello(name: &str) {
}
```

<span class="caption">Listing 15-13: A `hello` function that has the parameter
<span class="caption">Listing 15-11: A `hello` function that has the parameter
`name` of type `&str`</span>

We can call the `hello` function with a string slice as an argument, like
`hello("Rust");` for example. Deref coercion makes it possible for us to call
`hello` with a reference to a value of type `MyBox<String>`, as shown in
Listing 15-14:
Listing 15-12:

<span class="filename">Filename: src/main.rs</span>

Expand Down Expand Up @@ -286,20 286,20 @@ fn main() {
}
```

<span class="caption">Listing 15-14: Calling `hello` with a reference to a
<span class="caption">Listing 15-12: Calling `hello` with a reference to a
`MyBox<String>`, which works because of deref coercion</span>

Here we’re calling the `hello` function with the argument `&m`, which is a
reference to a `MyBox<String>` value. Because we implemented the `Deref` trait
on `MyBox<T>` in Listing 15-12, Rust can turn `&MyBox<String>` into `&String`
on `MyBox<T>` in Listing 15-10, Rust can turn `&MyBox<String>` into `&String`
by calling `deref`. The standard library provides an implementation of `Deref`
on `String` that returns a string slice, which we can see in the API
documentation for `Deref`. Rust calls `deref` again to turn the `&String` into
`&str`, which matches the `hello` function’s definition.

If Rust didn’t implement deref coercion, in order to call `hello` with a value
of type `&MyBox<String>`, we’d have to write the code in Listing 15-15 instead
of the code in Listing 15-14:
of type `&MyBox<String>`, we’d have to write the code in Listing 15-13 instead
of the code in Listing 15-12:

<span class="filename">Filename: src/main.rs</span>

Expand Down Expand Up @@ -332,7 332,7 @@ fn main() {
}
```

<span class="caption">Listing 15-15: The code we’d have to write if Rust didn’t
<span class="caption">Listing 15-13: The code we’d have to write if Rust didn’t
have deref coercion</span>

The `(*m)` is dereferencing the `MyBox<String>` into a `String`. Then the `&`
Expand Down
12 changes: 6 additions & 6 deletions second-edition/src/ch15-03-drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 24,7 @@ We specify the code to run when a value goes out of scope by implementing the
that takes a mutable reference to `self`. In order to be able to see when Rust
calls `drop`, let’s implement `drop` with `println!` statements for now.

Listing 15-16 shows a `CustomSmartPointer` struct whose only custom
Listing 15-14 shows a `CustomSmartPointer` struct whose only custom
functionality is that it will print out `Dropping CustomSmartPointer!` when the
instance goes out of scope. This will demonstrate when Rust runs the `drop`
function:
Expand All @@ -49,7 49,7 @@ fn main() {
}
```

<span class="caption">Listing 15-16: A `CustomSmartPointer` struct that
<span class="caption">Listing 15-14: A `CustomSmartPointer` struct that
implements the `Drop` trait, where we would put our clean up code.</span>

The `Drop` trait is included in the prelude, so we don’t need to import it. We
Expand Down Expand Up @@ -90,7 90,7 @@ up a value early. One example is when using smart pointers that manage locks;
you may want to force the `drop` method that releases the lock to run so that
other code in the same scope can acquire the lock. First, let’s see what
happens if we try to call the `Drop` trait’s `drop` method ourselves by
modifying the `main` function from Listing 15-16 as shown in Listing 15-17:
modifying the `main` function from Listing 15-14 as shown in Listing 15-15:

<span class="filename">Filename: src/main.rs</span>

Expand All @@ -103,7 103,7 @@ fn main() {
}
```

<span class="caption">Listing 15-17: Attempting to call the `drop` method from
<span class="caption">Listing 15-15: Attempting to call the `drop` method from
the `Drop` trait manually to clean up early</span>

If we try to compile this, we’ll get this error:
Expand Down Expand Up @@ -134,7 134,7 @@ force a value to be cleaned up early, we can use the `std::mem::drop` function.
The `std::mem::drop` function is different than the `drop` method in the `Drop`
trait. We call it by passing the value we want to force to be dropped early as
an argument. `std::mem::drop` is in the prelude, so we can modify `main` from
Listing 15-16 to call the `drop` function as shown in Listing 15-18:
Listing 15-14 to call the `drop` function as shown in Listing 15-16:

<span class="filename">Filename: src/main.rs</span>

Expand All @@ -157,7 157,7 @@ fn main() {
}
```

<span class="caption">Listing 15-18: Calling `std::mem::drop` to explicitly
<span class="caption">Listing 15-16: Calling `std::mem::drop` to explicitly
drop a value before it goes out of scope</span>

Running this code will print the following:
Expand Down
20 changes: 10 additions & 10 deletions second-edition/src/ch15-04-rc.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 30,13 @@ concurrency will cover how to do reference counting in multithreaded programs.

### Using `Rc<T>` to Share Data

Let’s return to our cons list example from Listing 15-6, as we defined it using
Let’s return to our cons list example from Listing 15-5, as we defined it using
`Box<T>`. This time, we want to create two lists that both share ownership of a
third list, which conceptually will look something like Figure 15-19:
third list, which conceptually will look something like Figure 15-3:

<img alt="Two lists that share ownership of a third list" src="img/trpl15-03.svg" class="center" />

<span class="caption">Figure 15-19: Two lists, `b` and `c`, sharing ownership
<span class="caption">Figure 15-3: Two lists, `b` and `c`, sharing ownership
of a third list, `a`</span>

We’ll create list `a` that contains 5 and then 10, then make two more lists:
Expand All @@ -45,7 45,7 @@ then continue on to the first `a` list containing 5 and 10. In other words,
both lists will try to share the first list containing 5 and 10.

Trying to implement this using our definition of `List` with `Box<T>` won’t
work, as shown in Listing 15-20:
work, as shown in Listing 15-17:

<span class="filename">Filename: src/main.rs</span>

Expand All @@ -66,7 66,7 @@ fn main() {
}
```

<span class="caption">Listing 15-20: Demonstrating we’re not allowed to have
<span class="caption">Listing 15-17: Demonstrating we’re not allowed to have
two lists using `Box<T>` that try to share ownership of a third list</span>

If we compile this, we get this error:
Expand Down Expand Up @@ -96,7 96,7 @@ the list itself. The borrow checker wouldn’t let us compile `let a = Cons(10,
`a` could take a reference to it.

Instead, we’ll change our definition of `List` to use `Rc<T>` in place of
`Box<T>` as shown here in Listing 15-21. Each `Cons` variant now holds a value
`Box<T>` as shown here in Listing 15-18. Each `Cons` variant now holds a value
and an `Rc` pointing to a `List`. When we create `b`, instead of taking
ownership of `a`, we clone the `Rc` that `a` is holding, which increases the
number of references from 1 to 2 and lets `a` and `b` share ownership of the
Expand All @@ -123,7 123,7 @@ fn main() {
}
```

<span class="caption">Listing 15-21: A definition of `List` that uses
<span class="caption">Listing 15-18: A definition of `List` that uses
`Rc<T>`</span>

We need to add a `use` statement to bring `Rc` into scope because it’s not in
Expand All @@ -144,10 144,10 @@ memory.

### Cloning an `Rc<T>` Increases the Reference Count

Let’s change our working example from Listing 15-21 so that we can see the
Let’s change our working example from Listing 15-18 so that we can see the
reference counts changing as we create and drop references to the `Rc` in `a`.

In Listing 15-22, we’ll change `main` so that it has an inner scope around list
In Listing 15-19, we’ll change `main` so that it has an inner scope around list
`c`, so that we can see how the reference count changes when `c` goes out of
scope. At each point in the program where the reference count changes, we’ll
print out the reference count, which we can get by calling the
Expand Down Expand Up @@ -179,7 179,7 @@ fn main() {
}
```

<span class="caption">Listing 15-22: Printing out the reference count</span>
<span class="caption">Listing 15-19: Printing out the reference count</span>

This will print out:

Expand Down
Loading

0 comments on commit 1d532d5

Please sign in to comment.