You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/ch18-01-what-is-oo.md
+5-6Lines changed: 5 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -44,30 +44,29 @@ on demand whenever anyone needs it. In other words, `AveragedCollection` will
44
44
cache the calculated average for us. Listing 18-1 has the definition of the
45
45
`AveragedCollection` struct:
46
46
47
-
<spanclass="filename">Filename: src/lib.rs</span>
47
+
<Listingnumber="18-1"file-name="src/lib.rs"caption="Listing 18-1: An `AveragedCollection` struct that maintains a list of integers and the average of the items in the collection">
<spanclass="caption">Listing 18-1: An `AveragedCollection` struct that
54
-
maintains a list of integers and the average of the items in the
55
-
collection</span>
53
+
</Listing>
56
54
57
55
The struct is marked `pub` so that other code can use it, but the fields within
58
56
the struct remain private. This is important in this case because we want to
59
57
ensure that whenever a value is added or removed from the list, the average is
60
58
also updated. We do this by implementing `add`, `remove`, and `average` methods
61
59
on the struct, as shown in Listing 18-2:
62
60
61
+
<Listingnumber="18-2"file-name="src/lib.rs"caption="Listing 18-2: Implementations of the public methods `add`, `remove`, and `average` on `AveragedCollection`">
<spanclass="caption">Listing 18-3: Definition of the `Draw` trait</span>
75
+
</Listing>
76
76
77
77
This syntax should look familiar from our discussions on how to define traits
78
78
in Chapter 10. Next comes some new syntax: Listing 18-4 defines a struct named
79
79
`Screen` that holds a vector named `components`. This vector is of type
80
80
`Box<dyn Draw>`, which is a trait object; it’s a stand-in for any type inside
81
81
a `Box` that implements the `Draw` trait.
82
82
83
-
<spanclass="filename">Filename: src/lib.rs</span>
83
+
<Listingnumber="18-4"file-name="src/lib.rs"caption="Definition of the `Screen` struct with a `components` field holding a vector of trait objects that implement the `Draw` trait">
<spanclass="caption">Listing 18-5: A `run` method on `Screen` that calls the
103
-
`draw` method on each component</span>
100
+
</Listing>
104
101
105
102
This works differently from defining a struct that uses a generic type
106
103
parameter with trait bounds. A generic type parameter can only be substituted
@@ -109,14 +106,13 @@ concrete types to fill in for the trait object at runtime. For example, we
109
106
could have defined the `Screen` struct using a generic type and a trait bound
110
107
as in Listing 18-6:
111
108
112
-
<spanclass="filename">Filename: src/lib.rs</span>
109
+
<Listingnumber="18-6"file-name="src/lib.rs"caption="An alternate implementation of the `Screen` struct and its `run` method using generics and trait bounds">
<spanclass="caption">Listing 18-11: Code that demonstrates the desired
50
-
behavior we want our `blog` crate to have</span>
49
+
</Listing>
51
50
52
51
We want to allow the user to create a new draft blog post with `Post::new`. We
53
52
want to allow text to be added to the blog post. If we try to get the post’s
@@ -84,15 +83,13 @@ Then `Post` will hold a trait object of `Box<dyn State>` inside an `Option<T>`
84
83
in a private field named `state` to hold the state object. You’ll see why the
85
84
`Option<T>` is necessary in a bit.
86
85
87
-
<spanclass="filename">Filename: src/lib.rs</span>
86
+
<Listingnumber="18-12"file-name="src/lib.rs"caption="Definition of a `Post` struct and a `new` function that creates a new `Post` instance, a `State` trait, and a `Draft` struct">
<spanclass="caption">Listing 18-13: Implementing the `add_text` method to add
127
-
text to a post’s `content`</span>
123
+
</Listing>
128
124
129
125
The `add_text` method takes a mutable reference to `self`, because we’re
130
126
changing the `Post` instance that we’re calling `add_text` on. We then call
@@ -145,14 +141,13 @@ once we implement the ability to change a post’s state so it can be published.
145
141
So far, posts can only be in the draft state, so the post content should always
146
142
be empty. Listing 18-14 shows this placeholder implementation:
147
143
148
-
<spanclass="filename">Filename: src/lib.rs</span>
144
+
<Listingnumber="18-14"file-name="src/lib.rs"caption="Adding a placeholder implementation for the `content` method on `Post` that always returns an empty string slice">
<spanclass="caption">Listing 18-19: A `Post` with a `content` method and a
409
-
`DraftPost` without a `content` method</span>
401
+
</Listing>
410
402
411
403
Both the `Post` and `DraftPost` structs have a private `content` field that
412
404
stores the blog post text. The structs no longer have the `state` field because
@@ -435,15 +427,13 @@ these constraints by adding another struct, `PendingReviewPost`, defining the
435
427
defining an `approve` method on `PendingReviewPost` to return a `Post`, as
436
428
shown in Listing 18-20:
437
429
438
-
<spanclass="filename">Filename: src/lib.rs</span>
430
+
<Listingnumber="18-20"file-name="src/lib.rs"caption="A `PendingReviewPost` that gets created by calling `request_review` on `DraftPost` and an `approve` method that turns a `PendingReviewPost` into a published `Post`">
0 commit comments