@@ -17,15 +17,16 @@ define a set of behaviors necessary to accomplish some purpose.
17
17
18
18
For example, let’s say we have multiple structs that hold various kinds and
19
19
amounts of text: a ` NewsArticle ` struct that holds a news story filed in a
20
- particular location and a ` Tweet ` that can have, at most, 280 characters along
21
- with metadata that indicates whether it was a new tweet , a retweet , or a reply
22
- to another tweet .
20
+ particular location and a ` SocialPost ` that can have, at most, 280 characters
21
+ along with metadata that indicates whether it was a new post , a repost , or a
22
+ reply to another post .
23
23
24
24
We want to make a media aggregator library crate named ` aggregator ` that can
25
- display summaries of data that might be stored in a ` NewsArticle ` or ` Tweet `
26
- instance. To do this, we need a summary from each type, and we’ll request that
27
- summary by calling a ` summarize ` method on an instance. Listing 10-12 shows the
28
- definition of a public ` Summary ` trait that expresses this behavior.
25
+ display summaries of data that might be stored in a ` NewsArticle ` or
26
+ ` SocialPost ` instance. To do this, we need a summary from each type, and we’ll
27
+ request that summary by calling a ` summarize ` method on an instance. Listing
28
+ 10-12 shows the definition of a public ` Summary ` trait that expresses this
29
+ behavior.
29
30
30
31
<Listing number =" 10-12 " file-name =" src/lib.rs " caption =" A `Summary` trait that consists of the behavior provided by a `summarize` method " >
31
32
@@ -57,11 +58,11 @@ Now that we’ve defined the desired signatures of the `Summary` trait’s metho
57
58
we can implement it on the types in our media aggregator. Listing 10-13 shows
58
59
an implementation of the ` Summary ` trait on the ` NewsArticle ` struct that uses
59
60
the headline, the author, and the location to create the return value of
60
- ` summarize ` . For the ` Tweet ` struct, we define ` summarize ` as the username
61
- followed by the entire text of the tweet , assuming that the tweet content is
61
+ ` summarize ` . For the ` SocialPost ` struct, we define ` summarize ` as the username
62
+ followed by the entire text of the post , assuming that the post content is
62
63
already limited to 280 characters.
63
64
64
- <Listing number =" 10-13 " file-name =" src/lib.rs " caption =" Implementing the `Summary` trait on the `NewsArticle` and `Tweet ` types " >
65
+ <Listing number =" 10-13 " file-name =" src/lib.rs " caption =" Implementing the `Summary` trait on the `NewsArticle` and `SocialPost ` types " >
65
66
66
67
``` rust,noplayground
67
68
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs:here}}
@@ -78,8 +79,8 @@ signature, we use curly brackets and fill in the method body with the specific
78
79
behavior that we want the methods of the trait to have for the particular type.
79
80
80
81
Now that the library has implemented the ` Summary ` trait on ` NewsArticle ` and
81
- ` Tweet ` , users of the crate can call the trait methods on instances of
82
- ` NewsArticle ` and ` Tweet ` in the same way we call regular methods. The only
82
+ ` SocialPost ` , users of the crate can call the trait methods on instances of
83
+ ` NewsArticle ` and ` SocialPost ` in the same way we call regular methods. The only
83
84
difference is that the user must bring the trait into scope as well as the
84
85
types. Here’s an example of how a binary crate could use our ` aggregator `
85
86
library crate:
@@ -88,15 +89,15 @@ library crate:
88
89
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs}}
89
90
```
90
91
91
- This code prints `1 new tweet : horse_ebooks: of course, as you probably already
92
+ This code prints `1 new post : horse_ebooks: of course, as you probably already
92
93
know, people`.
93
94
94
95
Other crates that depend on the ` aggregator ` crate can also bring the ` Summary `
95
96
trait into scope to implement ` Summary ` on their own types. One restriction to
96
97
note is that we can implement a trait on a type only if either the trait or the
97
98
type, or both, are local to our crate. For example, we can implement standard
98
- library traits like ` Display ` on a custom type like ` Tweet ` as part of our
99
- ` aggregator ` crate functionality because the type ` Tweet ` is local to our
99
+ library traits like ` Display ` on a custom type like ` SocialPost ` as part of our
100
+ ` aggregator ` crate functionality because the type ` SocialPost ` is local to our
100
101
` aggregator ` crate. We can also implement ` Summary ` on ` Vec<T> ` in our
101
102
` aggregator ` crate because the trait ` Summary ` is local to our ` aggregator `
102
103
crate.
@@ -145,9 +146,10 @@ the `summarize` method on an instance of `NewsArticle`, like this:
145
146
This code prints ` New article available! (Read more...) ` .
146
147
147
148
Creating a default implementation doesn’t require us to change anything about
148
- the implementation of ` Summary ` on ` Tweet ` in Listing 10-13. The reason is that
149
- the syntax for overriding a default implementation is the same as the syntax
150
- for implementing a trait method that doesn’t have a default implementation.
149
+ the implementation of ` Summary ` on ` SocialPost ` in Listing 10-13. The reason is
150
+ that the syntax for overriding a default implementation is the same as the
151
+ syntax for implementing a trait method that doesn’t have a default
152
+ implementation.
151
153
152
154
Default implementations can call other methods in the same trait, even if those
153
155
other methods don’t have a default implementation. In this way, a trait can
@@ -169,7 +171,7 @@ when we implement the trait on a type:
169
171
```
170
172
171
173
After we define ` summarize_author ` , we can call ` summarize ` on instances of the
172
- ` Tweet ` struct, and the default implementation of ` summarize ` will call the
174
+ ` SocialPost ` struct, and the default implementation of ` summarize ` will call the
173
175
definition of ` summarize_author ` that we’ve provided. Because we’ve implemented
174
176
` summarize_author ` , the ` Summary ` trait has given us the behavior of the
175
177
` summarize ` method without requiring us to write any more code. Here’s what
@@ -179,7 +181,7 @@ that looks like:
179
181
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs:here}}
180
182
```
181
183
182
- This code prints ` 1 new tweet : (Read more from @horse_ebooks...) ` .
184
+ This code prints ` 1 new post : (Read more from @horse_ebooks...) ` .
183
185
184
186
Note that it isn’t possible to call the default implementation from an
185
187
overriding implementation of that same method.
@@ -188,7 +190,7 @@ overriding implementation of that same method.
188
190
189
191
Now that you know how to define and implement traits, we can explore how to use
190
192
traits to define functions that accept many different types. We’ll use the
191
- ` Summary ` trait we implemented on the ` NewsArticle ` and ` Tweet ` types in
193
+ ` Summary ` trait we implemented on the ` NewsArticle ` and ` SocialPost ` types in
192
194
Listing 10-13 to define a ` notify ` function that calls the ` summarize ` method
193
195
on its ` item ` parameter, which is of some type that implements the ` Summary `
194
196
trait. To do this, we use the ` impl Trait ` syntax, like this:
@@ -201,7 +203,7 @@ Instead of a concrete type for the `item` parameter, we specify the `impl`
201
203
keyword and the trait name. This parameter accepts any type that implements the
202
204
specified trait. In the body of ` notify ` , we can call any methods on ` item `
203
205
that come from the ` Summary ` trait, such as ` summarize ` . We can call ` notify `
204
- and pass in any instance of ` NewsArticle ` or ` Tweet ` . Code that calls the
206
+ and pass in any instance of ` NewsArticle ` or ` SocialPost ` . Code that calls the
205
207
function with any other type, such as a ` String ` or an ` i32 ` , won’t compile
206
208
because those types don’t implement ` Summary ` .
207
209
@@ -301,7 +303,8 @@ value of some type that implements a trait, as shown here:
301
303
By using ` impl Summary ` for the return type, we specify that the
302
304
` returns_summarizable ` function returns some type that implements the ` Summary `
303
305
trait without naming the concrete type. In this case, ` returns_summarizable `
304
- returns a ` Tweet ` , but the code calling this function doesn’t need to know that.
306
+ returns a ` SocialPost ` , but the code calling this function doesn’t need to know
307
+ that.
305
308
306
309
The ability to specify a return type only by the trait it implements is
307
310
especially useful in the context of closures and iterators, which we cover in
@@ -311,19 +314,19 @@ specify that a function returns some type that implements the `Iterator` trait
311
314
without needing to write out a very long type.
312
315
313
316
However, you can only use ` impl Trait ` if you’re returning a single type. For
314
- example, this code that returns either a ` NewsArticle ` or a ` Tweet ` with the
315
- return type specified as ` impl Summary ` wouldn’t work:
317
+ example, this code that returns either a ` NewsArticle ` or a ` SocialPost ` with
318
+ the return type specified as ` impl Summary ` wouldn’t work:
316
319
317
320
``` rust,ignore,does_not_compile
318
321
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs:here}}
319
322
```
320
323
321
- Returning either a ` NewsArticle ` or a ` Tweet ` isn’t allowed due to restrictions
322
- around how the ` impl Trait ` syntax is implemented in the compiler. We’ll cover
323
- how to write a function with this behavior in the [ “Using Trait Objects That
324
- Allow for Values of Different
325
- Types”] [ using-trait-objects-that-allow-for-values-of-different-types ] <!--
326
- ignore --> section of Chapter 18.
324
+ Returning either a ` NewsArticle ` or a ` SocialPost ` isn’t allowed due to
325
+ restrictions around how the ` impl Trait ` syntax is implemented in the compiler.
326
+ We’ll cover how to write a function with this behavior in the [ “Using Trait
327
+ Objects That Allow for Values of Different
328
+ Types”] [ using-trait-objects-that-allow-for-values-of-different-types ] <!-- ignore
329
+ --> section of Chapter 18.
327
330
328
331
### Using Trait Bounds to Conditionally Implement Methods
329
332
0 commit comments