Skip to content

Commit 3ebd66f

Browse files
committed
Ch. 10: Make social media discussion generic.
Twitter is now X, who knows what 'tweets' are called, and whether *any* of the current crop of social media platforms will exist by the time the 2027 or 2030 editions of Rust roll around, who knows?
1 parent 0896670 commit 3ebd66f

File tree

12 files changed

+71
-67
lines changed

12 files changed

+71
-67
lines changed

ci/dictionary.txt

+1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ sizeof
487487
SliceIndex
488488
Smalltalk
489489
snuck
490+
SocialPost
490491
someproject
491492
SomeType
492493
someusername

listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ impl Summary for NewsArticle {
1616
}
1717
}
1818

19-
pub struct Tweet {
19+
pub struct SocialPost {
2020
pub username: String,
2121
pub content: String,
2222
pub reply: bool,
23-
pub retweet: bool,
23+
pub repost: bool,
2424
}
2525

26-
impl Summary for Tweet {
26+
impl Summary for SocialPost {
2727
fn summarize(&self) -> String {
2828
format!("{}: {}", self.username, self.content)
2929
}

listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ pub struct NewsArticle {
1515

1616
impl Summary for NewsArticle {}
1717

18-
pub struct Tweet {
18+
pub struct SocialPost {
1919
pub username: String,
2020
pub content: String,
2121
pub reply: bool,
22-
pub retweet: bool,
22+
pub repost: bool,
2323
}
2424

25-
impl Summary for Tweet {
25+
impl Summary for SocialPost {
2626
fn summarize(&self) -> String {
2727
format!("{}: {}", self.username, self.content)
2828
}

listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ impl Summary for NewsArticle {
1515
}
1616
}
1717

18-
pub struct Tweet {
18+
pub struct SocialPost {
1919
pub username: String,
2020
pub content: String,
2121
pub reply: bool,
22-
pub retweet: bool,
22+
pub repost: bool,
2323
}
2424

25-
impl Summary for Tweet {
25+
impl Summary for SocialPost {
2626
fn summarize(&self) -> String {
2727
format!("{}: {}", self.username, self.content)
2828
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use aggregator::{Summary, Tweet};
1+
use aggregator::{SocialPost, Summary};
22

33
fn main() {
4-
let tweet = Tweet {
4+
let post = SocialPost {
55
username: String::from("horse_ebooks"),
66
content: String::from(
77
"of course, as you probably already know, people",
88
),
99
reply: false,
10-
retweet: false,
10+
repost: false,
1111
};
1212

13-
println!("1 new tweet: {}", tweet.summarize());
13+
println!("1 new social post: {}", post.summarize());
1414
}

listings/ch10-generic-types-traits-and-lifetimes/no-listing-02-calling-default-impl/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ pub struct NewsArticle {
1313

1414
impl Summary for NewsArticle {}
1515

16-
pub struct Tweet {
16+
pub struct SocialPost {
1717
pub username: String,
1818
pub content: String,
1919
pub reply: bool,
20-
pub retweet: bool,
20+
pub repost: bool,
2121
}
2222

23-
impl Summary for Tweet {
23+
impl Summary for SocialPost {
2424
fn summarize(&self) -> String {
2525
format!("{}: {}", self.username, self.content)
2626
}

listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ pub trait Summary {
88
}
99
// ANCHOR_END: here
1010

11-
pub struct Tweet {
11+
pub struct SocialPost {
1212
pub username: String,
1313
pub content: String,
1414
pub reply: bool,
15-
pub retweet: bool,
15+
pub repost: bool,
1616
}
1717

1818
// ANCHOR: impl
19-
impl Summary for Tweet {
19+
impl Summary for SocialPost {
2020
fn summarize_author(&self) -> String {
2121
format!("@{}", self.username)
2222
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use aggregator::{self, Summary, Tweet};
1+
use aggregator::{self, SocialPost, Summary};
22

33
fn main() {
44
// ANCHOR: here
5-
let tweet = Tweet {
5+
let post = SocialPost {
66
username: String::from("horse_ebooks"),
77
content: String::from(
88
"of course, as you probably already know, people",
99
),
1010
reply: false,
11-
retweet: false,
11+
repost: false,
1212
};
1313

14-
println!("1 new tweet: {}", tweet.summarize());
14+
println!("1 new social post: {}", post.summarize());
1515
// ANCHOR_END: here
1616
}

listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ impl Summary for NewsArticle {
1515
}
1616
}
1717

18-
pub struct Tweet {
18+
pub struct SocialPost {
1919
pub username: String,
2020
pub content: String,
2121
pub reply: bool,
22-
pub retweet: bool,
22+
pub repost: bool,
2323
}
2424

25-
impl Summary for Tweet {
25+
impl Summary for SocialPost {
2626
fn summarize(&self) -> String {
2727
format!("{}: {}", self.username, self.content)
2828
}

listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ impl Summary for NewsArticle {
1515
}
1616
}
1717

18-
pub struct Tweet {
18+
pub struct SocialPost {
1919
pub username: String,
2020
pub content: String,
2121
pub reply: bool,
22-
pub retweet: bool,
22+
pub repost: bool,
2323
}
2424

25-
impl Summary for Tweet {
25+
impl Summary for SocialPost {
2626
fn summarize(&self) -> String {
2727
format!("{}: {}", self.username, self.content)
2828
}
2929
}
3030

3131
// ANCHOR: here
3232
fn returns_summarizable() -> impl Summary {
33-
Tweet {
33+
SocialPost {
3434
username: String::from("horse_ebooks"),
3535
content: String::from(
3636
"of course, as you probably already know, people",
3737
),
3838
reply: false,
39-
retweet: false,
39+
repost: false,
4040
}
4141
}
4242
// ANCHOR_END: here

listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ impl Summary for NewsArticle {
1515
}
1616
}
1717

18-
pub struct Tweet {
18+
pub struct SocialPost {
1919
pub username: String,
2020
pub content: String,
2121
pub reply: bool,
22-
pub retweet: bool,
22+
pub repost: bool,
2323
}
2424

25-
impl Summary for Tweet {
25+
impl Summary for SocialPost {
2626
fn summarize(&self) -> String {
2727
format!("{}: {}", self.username, self.content)
2828
}
@@ -43,13 +43,13 @@ fn returns_summarizable(switch: bool) -> impl Summary {
4343
),
4444
}
4545
} else {
46-
Tweet {
46+
SocialPost {
4747
username: String::from("horse_ebooks"),
4848
content: String::from(
4949
"of course, as you probably already know, people",
5050
),
5151
reply: false,
52-
retweet: false,
52+
repost: false,
5353
}
5454
}
5555
}

src/ch10-02-traits.md

+34-31
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ define a set of behaviors necessary to accomplish some purpose.
1717

1818
For example, let’s say we have multiple structs that hold various kinds and
1919
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.
2323

2424
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.
2930

3031
<Listing number="10-12" file-name="src/lib.rs" caption="A `Summary` trait that consists of the behavior provided by a `summarize` method">
3132

@@ -57,11 +58,11 @@ Now that we’ve defined the desired signatures of the `Summary` trait’s metho
5758
we can implement it on the types in our media aggregator. Listing 10-13 shows
5859
an implementation of the `Summary` trait on the `NewsArticle` struct that uses
5960
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
6263
already limited to 280 characters.
6364

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">
6566

6667
```rust,noplayground
6768
{{#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
7879
behavior that we want the methods of the trait to have for the particular type.
7980

8081
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
8384
difference is that the user must bring the trait into scope as well as the
8485
types. Here’s an example of how a binary crate could use our `aggregator`
8586
library crate:
@@ -88,15 +89,15 @@ library crate:
8889
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-01-calling-trait-method/src/main.rs}}
8990
```
9091

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
9293
know, people`.
9394

9495
Other crates that depend on the `aggregator` crate can also bring the `Summary`
9596
trait into scope to implement `Summary` on their own types. One restriction to
9697
note is that we can implement a trait on a type only if either the trait or the
9798
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
100101
`aggregator` crate. We can also implement `Summary` on `Vec<T>` in our
101102
`aggregator` crate because the trait `Summary` is local to our `aggregator`
102103
crate.
@@ -145,9 +146,10 @@ the `summarize` method on an instance of `NewsArticle`, like this:
145146
This code prints `New article available! (Read more...)`.
146147

147148
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.
151153

152154
Default implementations can call other methods in the same trait, even if those
153155
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:
169171
```
170172

171173
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
173175
definition of `summarize_author` that we’ve provided. Because we’ve implemented
174176
`summarize_author`, the `Summary` trait has given us the behavior of the
175177
`summarize` method without requiring us to write any more code. Here’s what
@@ -179,7 +181,7 @@ that looks like:
179181
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-03-default-impl-calls-other-methods/src/main.rs:here}}
180182
```
181183

182-
This code prints `1 new tweet: (Read more from @horse_ebooks...)`.
184+
This code prints `1 new post: (Read more from @horse_ebooks...)`.
183185

184186
Note that it isn’t possible to call the default implementation from an
185187
overriding implementation of that same method.
@@ -188,7 +190,7 @@ overriding implementation of that same method.
188190

189191
Now that you know how to define and implement traits, we can explore how to use
190192
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
192194
Listing 10-13 to define a `notify` function that calls the `summarize` method
193195
on its `item` parameter, which is of some type that implements the `Summary`
194196
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`
201203
keyword and the trait name. This parameter accepts any type that implements the
202204
specified trait. In the body of `notify`, we can call any methods on `item`
203205
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
205207
function with any other type, such as a `String` or an `i32`, won’t compile
206208
because those types don’t implement `Summary`.
207209

@@ -301,7 +303,8 @@ value of some type that implements a trait, as shown here:
301303
By using `impl Summary` for the return type, we specify that the
302304
`returns_summarizable` function returns some type that implements the `Summary`
303305
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.
305308

306309
The ability to specify a return type only by the trait it implements is
307310
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
311314
without needing to write out a very long type.
312315

313316
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:
316319

317320
```rust,ignore,does_not_compile
318321
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs:here}}
319322
```
320323

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.
327330

328331
### Using Trait Bounds to Conditionally Implement Methods
329332

0 commit comments

Comments
 (0)