Skip to content

Commit 670dd6b

Browse files
committed
Backport copyedit changes to ch15
1 parent 7b51a04 commit 670dd6b

File tree

15 files changed

+503
-437
lines changed

15 files changed

+503
-437
lines changed

listings/ch15-smart-pointers/listing-15-14/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ $ cargo run
22
Compiling drop-example v0.1.0 (file:///projects/drop-example)
33
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.60s
44
Running `target/debug/drop-example`
5-
CustomSmartPointers created.
5+
CustomSmartPointers created
66
Dropping CustomSmartPointer with data `other stuff`!
77
Dropping CustomSmartPointer with data `my stuff`!

listings/ch15-smart-pointers/listing-15-14/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ fn main() {
1515
let d = CustomSmartPointer {
1616
data: String::from("other stuff"),
1717
};
18-
println!("CustomSmartPointers created.");
18+
println!("CustomSmartPointers created");
1919
}

listings/ch15-smart-pointers/listing-15-15/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ fn main() {
1313
let c = CustomSmartPointer {
1414
data: String::from("some data"),
1515
};
16-
println!("CustomSmartPointer created.");
16+
println!("CustomSmartPointer created");
1717
c.drop();
18-
println!("CustomSmartPointer dropped before the end of main.");
18+
println!("CustomSmartPointer dropped before the end of main");
1919
}
2020
// ANCHOR_END: here

listings/ch15-smart-pointers/listing-15-16/output.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ $ cargo run
22
Compiling drop-example v0.1.0 (file:///projects/drop-example)
33
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s
44
Running `target/debug/drop-example`
5-
CustomSmartPointer created.
5+
CustomSmartPointer created
66
Dropping CustomSmartPointer with data `some data`!
7-
CustomSmartPointer dropped before the end of main.
7+
CustomSmartPointer dropped before the end of main

listings/ch15-smart-pointers/listing-15-16/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ fn main() {
1313
let c = CustomSmartPointer {
1414
data: String::from("some data"),
1515
};
16-
println!("CustomSmartPointer created.");
16+
println!("CustomSmartPointer created");
1717
drop(c);
18-
println!("CustomSmartPointer dropped before the end of main.");
18+
println!("CustomSmartPointer dropped before the end of main");
1919
}
2020
// ANCHOR_END: here

listings/ch15-smart-pointers/listing-15-25/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ANCHOR: here
12
use crate::List::{Cons, Nil};
23
use std::cell::RefCell;
34
use std::rc::Rc;
@@ -16,5 +17,6 @@ impl List {
1617
}
1718
}
1819
}
20+
// ANCHOR_END: here
1921

2022
fn main() {}

nostarch/chapter15.md

Lines changed: 283 additions & 227 deletions
Large diffs are not rendered by default.

nostarch/docx/chapter15.docx

201 Bytes
Binary file not shown.

src/ch15-00-smart-pointers.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Smart Pointers
22

3-
A _pointer_ is a general concept for a variable that contains an address in
3+
A pointer is a general concept for a variable that contains an address in
44
memory. This address refers to, or “points at,” some other data. The most
55
common kind of pointer in Rust is a reference, which you learned about in
66
Chapter 4. References are indicated by the `&` symbol and borrow the value they
@@ -9,25 +9,25 @@ data, and they have no overhead.
99

1010
_Smart pointers_, on the other hand, are data structures that act like a
1111
pointer but also have additional metadata and capabilities. The concept of
12-
smart pointers isn’t unique to Rust: smart pointers originated in C++ and exist
12+
smart pointers isn’t unique to Rust: Smart pointers originated in C++ and exist
1313
in other languages as well. Rust has a variety of smart pointers defined in the
1414
standard library that provide functionality beyond that provided by references.
1515
To explore the general concept, we’ll look at a couple of different examples of
1616
smart pointers, including a _reference counting_ smart pointer type. This
1717
pointer enables you to allow data to have multiple owners by keeping track of
1818
the number of owners and, when no owners remain, cleaning up the data.
1919

20-
Rust, with its concept of ownership and borrowing, has an additional difference
21-
between references and smart pointers: while references only borrow data, in
22-
many cases smart pointers _own_ the data they point to.
20+
In Rust, with its concept of ownership and borrowing, there is an additional
21+
difference between references and smart pointers: While references only borrow
22+
data, in many cases smart pointers _own_ the data they point to.
2323

2424
Smart pointers are usually implemented using structs. Unlike an ordinary
2525
struct, smart pointers implement the `Deref` and `Drop` traits. The `Deref`
2626
trait allows an instance of the smart pointer struct to behave like a reference
27-
so you can write your code to work with either references or smart pointers.
28-
The `Drop` trait allows you to customize the code that’s run when an instance
29-
of the smart pointer goes out of scope. In this chapter, we’ll discuss both of
30-
these traits and demonstrate why they’re important to smart pointers.
27+
so that you can write your code to work with either references or smart
28+
pointers. The `Drop` trait allows you to customize the code that’s run when an
29+
instance of the smart pointer goes out of scope. In this chapter, we’ll discuss
30+
both of these traits and demonstrate why they’re important to smart pointers.
3131

3232
Given that the smart pointer pattern is a general design pattern used
3333
frequently in Rust, this chapter won’t cover every existing smart pointer. Many

src/ch15-01-box.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ Boxes don’t have performance overhead, other than storing their data on the
99
heap instead of on the stack. But they don’t have many extra capabilities
1010
either. You’ll use them most often in these situations:
1111

12-
- When you have a type whose size can’t be known at compile time and you want
12+
- When you have a type whose size can’t be known at compile time, and you want
1313
to use a value of that type in a context that requires an exact size
14-
- When you have a large amount of data and you want to transfer ownership but
15-
ensure the data won’t be copied when you do so
16-
- When you want to own a value and you care only that it’s a type that
14+
- When you have a large amount of data, and you want to transfer ownership but
15+
ensure that the data won’t be copied when you do so
16+
- When you want to own a value, and you care only that it’s a type that
1717
implements a particular trait rather than being of a specific type
1818

1919
We’ll demonstrate the first situation in [“Enabling Recursive Types with
@@ -23,9 +23,9 @@ because the data is copied around on the stack. To improve performance in this
2323
situation, we can store the large amount of data on the heap in a box. Then,
2424
only the small amount of pointer data is copied around on the stack, while the
2525
data it references stays in one place on the heap. The third case is known as a
26-
_trait object_, and [“Using Trait Objects That Allow for Values of Different
27-
Types,][trait-objects]<!-- ignore --> in Chapter 18 is devoted to that topic.
28-
So what you learn here you’ll apply again in that section!
26+
_trait object_, and [“Using Trait Objects to Abstract over Shared
27+
Behavior][trait-objects]<!-- ignore --> in Chapter 18 is devoted to that
28+
topic. So, what you learn here you’ll apply again in that section!
2929

3030
<!-- Old headings. Do not remove or links may break. -->
3131

@@ -69,10 +69,10 @@ types could theoretically continue infinitely, so Rust can’t know how much spa
6969
the value needs. Because boxes have a known size, we can enable recursive types
7070
by inserting a box in the recursive type definition.
7171

72-
As an example of a recursive type, let’s explore the _cons list_. This is a data
72+
As an example of a recursive type, let’s explore the cons list. This is a data
7373
type commonly found in functional programming languages. The cons list type
7474
we’ll define is straightforward except for the recursion; therefore, the
75-
concepts in the example we’ll work with will be useful any time you get into
75+
concepts in the example we’ll work with will be useful anytime you get into
7676
more complex situations involving recursive types.
7777

7878
<!-- Old headings. Do not remove or links may break. -->
@@ -96,11 +96,11 @@ list `1, 2, 3` with each pair in parentheses:
9696
```
9797

9898
Each item in a cons list contains two elements: the value of the current item
99-
and the next item. The last item in the list contains only a value called `Nil`
100-
without a next item. A cons list is produced by recursively calling the `cons`
101-
function. The canonical name to denote the base case of the recursion is `Nil`.
102-
Note that this is not the same as the “null” or “nil” concept discussed in
103-
Chapter 6, which is an invalid or absent value.
99+
and of the next item. The last item in the list contains only a value called
100+
`Nil` without a next item. A cons list is produced by recursively calling the
101+
`cons` function. The canonical name to denote the base case of the recursion is
102+
`Nil`. Note that this is not the same as the “null” or “nil” concept discussed
103+
in Chapter 6, which is an invalid or absent value.
104104

105105
The cons list isn’t a commonly used data structure in Rust. Most of the time
106106
when you have a list of items in Rust, `Vec<T>` is a better choice to use.
@@ -109,7 +109,7 @@ but by starting with the cons list in this chapter, we can explore how boxes
109109
let us define a recursive data type without much distraction.
110110

111111
Listing 15-2 contains an enum definition for a cons list. Note that this code
112-
won’t compile yet because the `List` type doesn’t have a known size, which
112+
won’t compile yet, because the `List` type doesn’t have a known size, which
113113
we’ll demonstrate.
114114

115115
<Listing number="15-2" file-name="src/main.rs" caption="The first attempt at defining an enum to represent a cons list data structure of `i32` values">
@@ -153,9 +153,9 @@ Listing 15-4.
153153
</Listing>
154154

155155
The error shows this type “has infinite size.” The reason is that we’ve defined
156-
`List` with a variant that is recursive: it holds another value of itself
156+
`List` with a variant that is recursive: It holds another value of itself
157157
directly. As a result, Rust can’t figure out how much space it needs to store a
158-
`List` value. Let’s break down why we get this error. First we’ll look at how
158+
`List` value. Let’s break down why we get this error. First, we’ll look at how
159159
Rust decides how much space it needs to store a value of a non-recursive type.
160160

161161
#### Computing the Size of a Non-Recursive Type
@@ -183,7 +183,7 @@ type needs, the compiler looks at the variants, starting with the `Cons`
183183
variant. The `Cons` variant holds a value of type `i32` and a value of type
184184
`List`, and this process continues infinitely, as shown in Figure 15-1.
185185

186-
<img alt="An infinite Cons list: a rectangle labeled 'Cons' split into two smaller rectangles. The first smaller rectangle holds the label 'i32', and the second smaller rectangle holds the label 'Cons' and a smaller version of the outer 'Cons' rectangle. The 'Cons' rectangles continue to hold smaller and smaller versions of themselves until the smallest comfortably-sized rectangle holds an infinity symbol, indicating that this repetition goes on forever" src="img/trpl15-01.svg" class="center" style="width: 50%;" />
186+
<img alt="An infinite Cons list: a rectangle labeled 'Cons' split into two smaller rectangles. The first smaller rectangle holds the label 'i32', an d the second smaller rectangle holds the label 'Cons' and a smaller version of the outer 'Cons' rectangle. The 'Cons' rectangles continue to hold sm aller and smaller versions of themselves until the smallest comfortably sized rectangle holds an infinity symbol, indicating that this repetition goes on forever." src="img/trpl15-01.svg" class="center" style="width: 50%;" />
187187

188188
<span class="caption">Figure 15-1: An infinite `List` consisting of infinite
189189
`Cons` variants</span>
@@ -213,7 +213,7 @@ directly, we should change the data structure to store the value indirectly by
213213
storing a pointer to the value instead.
214214

215215
Because a `Box<T>` is a pointer, Rust always knows how much space a `Box<T>`
216-
needs: a pointer’s size doesn’t change based on the amount of data it’s
216+
needs: A pointer’s size doesn’t change based on the amount of data it’s
217217
pointing to. This means we can put a `Box<T>` inside the `Cons` variant instead
218218
of another `List` value directly. The `Box<T>` will point to the next `List`
219219
value that will be on the heap rather than inside the `Cons` variant.
@@ -224,25 +224,25 @@ rather than inside one another.
224224
We can change the definition of the `List` enum in Listing 15-2 and the usage
225225
of the `List` in Listing 15-3 to the code in Listing 15-5, which will compile.
226226

227-
<Listing number="15-5" file-name="src/main.rs" caption="Definition of `List` that uses `Box<T>` in order to have a known size">
227+
<Listing number="15-5" file-name="src/main.rs" caption="The definition of `List` that uses `Box<T>` in order to have a known size">
228228

229229
```rust
230230
{{#rustdoc_include ../listings/ch15-smart-pointers/listing-15-05/src/main.rs}}
231231
```
232232

233233
</Listing>
234234

235-
The `Cons` variant needs the size of an `i32` plus the space to store the
236-
box’s pointer data. The `Nil` variant stores no values, so it needs less space
237-
on the stack than the `Cons` variant. We now know that any `List` value will
238-
take up the size of an `i32` plus the size of a box’s pointer data. By using a
239-
box, we’ve broken the infinite, recursive chain, so the compiler can figure out
240-
the size it needs to store a `List` value. Figure 15-2 shows what the `Cons`
235+
The `Cons` variant needs the size of an `i32` plus the space to store the box’s
236+
pointer data. The `Nil` variant stores no values, so it needs less space on the
237+
stack than the `Cons` variant. We now know that any `List` value will take up
238+
the size of an `i32` plus the size of a box’s pointer data. By using a box,
239+
we’ve broken the infinite, recursive chain, so the compiler can figure out the
240+
size it needs to store a `List` value. Figure 15-2 shows what the `Cons`
241241
variant looks like now.
242242

243-
<img alt="A rectangle labeled 'Cons' split into two smaller rectangles. The first smaller rectangle holds the label 'i32', and the second smaller rectangle holds the label 'Box' with one inner rectangle that contains the label 'usize', representing the finite size of the box's pointer" src="img/trpl15-02.svg" class="center" />
243+
<img alt="A rectangle labeled 'Cons' split into two smaller rectangles. The first smaller rectangle holds the label 'i32', and the second smaller rectangle holds the label 'Box' with one inner rectangle that contains the label 'usize', representing the finite size of the box's pointer." src="img/trpl15-02.svg" class="center" />
244244

245-
<span class="caption">Figure 15-2: A `List` that is not infinitely sized
245+
<span class="caption">Figure 15-2: A `List` that is not infinitely sized,
246246
because `Cons` holds a `Box`</span>
247247

248248
Boxes provide only the indirection and heap allocation; they don’t have any
@@ -260,4 +260,4 @@ even more important to the functionality provided by the other smart pointer
260260
types we’ll discuss in the rest of this chapter. Let’s explore these two traits
261261
in more detail.
262262

263-
[trait-objects]: ch18-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types
263+
[trait-objects]: ch18-02-trait-objects.html#using-trait-objects-to-abstract-over-shared-behavior

0 commit comments

Comments
 (0)