@@ -271,7 +271,7 @@ same type `T`.
271271Listing 10-5 shows the combined ` largest ` function definition using the generic
272272data type in its signature. The listing also shows how we can call the function
273273with either a slice of ` i32 ` values or ` char ` values. Note that this code won’t
274- compile yet, but we’ll fix it later in this chapter .
274+ compile yet.
275275
276276src/main.rs
277277
@@ -331,10 +331,10 @@ states that the body of `largest` won’t work for all possible types that `T`
331331could be. Because we want to compare values of type ` T ` in the body, we can
332332only use types whose values can be ordered. To enable comparisons, the standard
333333library has the ` std::cmp::PartialOrd ` trait that you can implement on types
334- (see Appendix C for more on this trait). By following the help text’s
335- suggestion, we restrict the types valid for ` T ` to only those that implement
336- ` PartialOrd ` and this example will compile, because the standard library
337- implements ` PartialOrd ` on both ` i32 ` and ` char ` .
334+ (see Appendix C for more on this trait). To fix Listing 10-5, we can follow the
335+ help text’s suggestion and restrict the types valid for ` T ` to only those that
336+ implement ` PartialOrd ` . The listing will then compile, because the standard
337+ library implements ` PartialOrd ` on both ` i32 ` and ` char ` .
338338
339339### In Struct Definitions
340340
@@ -669,15 +669,16 @@ define a set of behaviors necessary to accomplish some purpose.
669669
670670For example, let’s say we have multiple structs that hold various kinds and
671671amounts of text: a ` NewsArticle ` struct that holds a news story filed in a
672- particular location and a ` Tweet ` that can have, at most, 280 characters along
673- with metadata that indicates whether it was a new tweet , a retweet , or a reply
674- to another tweet .
672+ particular location and a ` SocialPost ` that can have, at most, 280 characters
673+ along with metadata that indicates whether it was a new post , a repost , or a
674+ reply to another post .
675675
676676We want to make a media aggregator library crate named ` aggregator ` that can
677- display summaries of data that might be stored in a ` NewsArticle ` or ` Tweet `
678- instance. To do this, we need a summary from each type, and we’ll request that
679- summary by calling a ` summarize ` method on an instance. Listing 10-12 shows the
680- definition of a public ` Summary ` trait that expresses this behavior.
677+ display summaries of data that might be stored in a ` NewsArticle ` or
678+ ` SocialPost ` instance. To do this, we need a summary from each type, and we’ll
679+ request that summary by calling a ` summarize ` method on an instance. Listing
680+ 10-12 shows the definition of a public ` Summary ` trait that expresses this
681+ behavior.
681682
682683src/lib.rs
683684
@@ -711,8 +712,8 @@ Now that we’ve defined the desired signatures of the `Summary` trait’s metho
711712we can implement it on the types in our media aggregator. Listing 10-13 shows
712713an implementation of the ` Summary ` trait on the ` NewsArticle ` struct that uses
713714the headline, the author, and the location to create the return value of
714- ` summarize ` . For the ` Tweet ` struct, we define ` summarize ` as the username
715- followed by the entire text of the tweet , assuming that the tweet content is
715+ ` summarize ` . For the ` SocialPost ` struct, we define ` summarize ` as the username
716+ followed by the entire text of the post , assuming that the post content is
716717already limited to 280 characters.
717718
718719src/lib.rs
@@ -731,21 +732,21 @@ impl Summary for NewsArticle {
731732 }
732733}
733734
734- pub struct Tweet {
735+ pub struct SocialPost {
735736 pub username: String,
736737 pub content: String,
737738 pub reply: bool,
738- pub retweet : bool,
739+ pub repost : bool,
739740}
740741
741- impl Summary for Tweet {
742+ impl Summary for SocialPost {
742743 fn summarize(&self) -> String {
743744 format!("{}: {}", self.username, self.content)
744745 }
745746}
746747```
747748
748- Listing 10-13: Implementing the ` Summary ` trait on the ` NewsArticle ` and ` Tweet ` types
749+ Listing 10-13: Implementing the ` Summary ` trait on the ` NewsArticle ` and ` SocialPost ` types
749750
750751Implementing a trait on a type is similar to implementing regular methods. The
751752difference is that after ` impl ` , we put the trait name we want to implement,
@@ -756,37 +757,37 @@ signature, we use curly brackets and fill in the method body with the specific
756757behavior that we want the methods of the trait to have for the particular type.
757758
758759Now that the library has implemented the ` Summary ` trait on ` NewsArticle ` and
759- ` Tweet ` , users of the crate can call the trait methods on instances of
760- ` NewsArticle ` and ` Tweet ` in the same way we call regular methods. The only
760+ ` SocialPost ` , users of the crate can call the trait methods on instances of
761+ ` NewsArticle ` and ` SocialPost ` in the same way we call regular methods. The only
761762difference is that the user must bring the trait into scope as well as the
762763types. Here’s an example of how a binary crate could use our ` aggregator `
763764library crate:
764765
765766```
766- use aggregator::{Summary, Tweet };
767+ use aggregator::{SocialPost, Summary };
767768
768769fn main() {
769- let tweet = Tweet {
770+ let post = SocialPost {
770771 username: String::from("horse_ebooks"),
771772 content: String::from(
772773 "of course, as you probably already know, people",
773774 ),
774775 reply: false,
775- retweet : false,
776+ repost : false,
776777 };
777778
778- println!("1 new tweet : {}", tweet .summarize());
779+ println!("1 new post : {}", post .summarize());
779780}
780781```
781782
782- This code prints ` 1 new tweet : horse_ebooks: of course, as you probably already know, people ` .
783+ This code prints ` 1 new post : horse_ebooks: of course, as you probably already know, people ` .
783784
784785Other crates that depend on the ` aggregator ` crate can also bring the ` Summary `
785786trait into scope to implement ` Summary ` on their own types. One restriction to
786787note is that we can implement a trait on a type only if either the trait or the
787788type, or both, are local to our crate. For example, we can implement standard
788- library traits like ` Display ` on a custom type like ` Tweet ` as part of our
789- ` aggregator ` crate functionality because the type ` Tweet ` is local to our
789+ library traits like ` Display ` on a custom type like ` SocialPost ` as part of our
790+ ` aggregator ` crate functionality because the type ` SocialPost ` is local to our
790791` aggregator ` crate. We can also implement ` Summary ` on ` Vec<T> ` in our
791792` aggregator ` crate because the trait ` Summary ` is local to our ` aggregator `
792793crate.
@@ -849,9 +850,10 @@ the `summarize` method on an instance of `NewsArticle`, like this:
849850This code prints ` New article available! (Read more...) ` .
850851
851852Creating a default implementation doesn’t require us to change anything about
852- the implementation of ` Summary ` on ` Tweet ` in Listing 10-13. The reason is that
853- the syntax for overriding a default implementation is the same as the syntax
854- for implementing a trait method that doesn’t have a default implementation.
853+ the implementation of ` Summary ` on ` SocialPost ` in Listing 10-13. The reason is
854+ that the syntax for overriding a default implementation is the same as the
855+ syntax for implementing a trait method that doesn’t have a default
856+ implementation.
855857
856858Default implementations can call other methods in the same trait, even if those
857859other methods don’t have a default implementation. In this way, a trait can
@@ -875,34 +877,34 @@ To use this version of `Summary`, we only need to define `summarize_author`
875877when we implement the trait on a type:
876878
877879```
878- impl Summary for Tweet {
880+ impl Summary for SocialPost {
879881 fn summarize_author(&self) -> String {
880882 format!("@{}", self.username)
881883 }
882884}
883885```
884886
885887After we define ` summarize_author ` , we can call ` summarize ` on instances of the
886- ` Tweet ` struct, and the default implementation of ` summarize ` will call the
888+ ` SocialPost ` struct, and the default implementation of ` summarize ` will call the
887889definition of ` summarize_author ` that we’ve provided. Because we’ve implemented
888890` summarize_author ` , the ` Summary ` trait has given us the behavior of the
889891` summarize ` method without requiring us to write any more code. Here’s what
890892that looks like:
891893
892894```
893- let tweet = Tweet {
895+ let post = SocialPost {
894896 username: String::from("horse_ebooks"),
895897 content: String::from(
896898 "of course, as you probably already know, people",
897899 ),
898900 reply: false,
899- retweet : false,
901+ repost : false,
900902 };
901903
902- println!("1 new tweet : {}", tweet .summarize());
904+ println!("1 new post : {}", post .summarize());
903905```
904906
905- This code prints ` 1 new tweet : (Read more from @horse_ebooks...) ` .
907+ This code prints ` 1 new post : (Read more from @horse_ebooks...) ` .
906908
907909Note that it isn’t possible to call the default implementation from an
908910overriding implementation of that same method.
@@ -911,7 +913,7 @@ overriding implementation of that same method.
911913
912914Now that you know how to define and implement traits, we can explore how to use
913915traits to define functions that accept many different types. We’ll use the
914- ` Summary ` trait we implemented on the ` NewsArticle ` and ` Tweet ` types in
916+ ` Summary ` trait we implemented on the ` NewsArticle ` and ` SocialPost ` types in
915917Listing 10-13 to define a ` notify ` function that calls the ` summarize ` method
916918on its ` item ` parameter, which is of some type that implements the ` Summary `
917919trait. To do this, we use the ` impl Trait ` syntax, like this:
@@ -926,7 +928,7 @@ Instead of a concrete type for the `item` parameter, we specify the `impl`
926928keyword and the trait name. This parameter accepts any type that implements the
927929specified trait. In the body of ` notify ` , we can call any methods on ` item `
928930that come from the ` Summary ` trait, such as ` summarize ` . We can call ` notify `
929- and pass in any instance of ` NewsArticle ` or ` Tweet ` . Code that calls the
931+ and pass in any instance of ` NewsArticle ` or ` SocialPost ` . Code that calls the
930932function with any other type, such as a ` String ` or an ` i32 ` , won’t compile
931933because those types don’t implement ` Summary ` .
932934
@@ -1025,21 +1027,22 @@ value of some type that implements a trait, as shown here:
10251027
10261028```
10271029fn returns_summarizable() -> impl Summary {
1028- Tweet {
1030+ SocialPost {
10291031 username: String::from("horse_ebooks"),
10301032 content: String::from(
10311033 "of course, as you probably already know, people",
10321034 ),
10331035 reply: false,
1034- retweet : false,
1036+ repost : false,
10351037 }
10361038}
10371039```
10381040
10391041By using ` impl Summary ` for the return type, we specify that the
10401042` returns_summarizable ` function returns some type that implements the ` Summary `
10411043trait without naming the concrete type. In this case, ` returns_summarizable `
1042- returns a ` Tweet ` , but the code calling this function doesn’t need to know that.
1044+ returns a ` SocialPost ` , but the code calling this function doesn’t need to know
1045+ that.
10431046
10441047The ability to specify a return type only by the trait it implements is
10451048especially useful in the context of closures and iterators, which we cover in
@@ -1049,8 +1052,8 @@ specify that a function returns some type that implements the `Iterator` trait
10491052without needing to write out a very long type.
10501053
10511054However, you can only use ` impl Trait ` if you’re returning a single type. For
1052- example, this code that returns either a ` NewsArticle ` or a ` Tweet ` with the
1053- return type specified as ` impl Summary ` wouldn’t work:
1055+ example, this code that returns either a ` NewsArticle ` or a ` SocialPost ` with
1056+ the return type specified as ` impl Summary ` wouldn’t work:
10541057
10551058```
10561059fn returns_summarizable(switch: bool) -> impl Summary {
@@ -1067,22 +1070,22 @@ fn returns_summarizable(switch: bool) -> impl Summary {
10671070 ),
10681071 }
10691072 } else {
1070- Tweet {
1073+ SocialPost {
10711074 username: String::from("horse_ebooks"),
10721075 content: String::from(
10731076 "of course, as you probably already know, people",
10741077 ),
10751078 reply: false,
1076- retweet : false,
1079+ repost : false,
10771080 }
10781081 }
10791082}
10801083```
10811084
1082- Returning either a ` NewsArticle ` or a ` Tweet ` isn’t allowed due to restrictions
1083- around how the ` impl Trait ` syntax is implemented in the compiler. We’ll cover
1084- how to write a function with this behavior in the “Using Trait Objects That
1085- Allow for Values of Different
1085+ Returning either a ` NewsArticle ` or a ` SocialPost ` isn’t allowed due to
1086+ restrictions around how the ` impl Trait ` syntax is implemented in the compiler.
1087+ We’ll cover how to write a function with this behavior in the “Using Trait
1088+ Objects That Allow for Values of Different
10861089Types” section of Chapter 18.
10871090
10881091### Using Trait Bounds to Conditionally Implement Methods
@@ -1173,11 +1176,12 @@ One detail we didn’t discuss in the “References and
11731176Borrowing” section in Chapter 4 is
11741177that every reference in Rust has a * lifetime* , which is the scope for which
11751178that reference is valid. Most of the time, lifetimes are implicit and inferred,
1176- just like most of the time, types are inferred. We must annotate types only
1177- when multiple types are possible. In a similar way, we must annotate lifetimes
1178- when the lifetimes of references could be related in a few different ways. Rust
1179- requires us to annotate the relationships using generic lifetime parameters to
1180- ensure the actual references used at runtime will definitely be valid.
1179+ just like most of the time, types are inferred. We are only required to
1180+ annotate types when multiple types are possible. In a similar way, we have to
1181+ annotate lifetimes when the lifetimes of references could be related in a few
1182+ different ways. Rust requires us to annotate the relationships using generic
1183+ lifetime parameters to ensure the actual references used at runtime will
1184+ definitely be valid.
11811185
11821186Annotating lifetimes is not even a concept most other programming languages
11831187have, so this is going to feel unfamiliar. Although we won’t cover lifetimes in
@@ -1338,11 +1342,7 @@ src/main.rs
13381342
13391343```
13401344fn longest(x: &str, y: &str) -> &str {
1341- if x.len() > y.len() {
1342- x
1343- } else {
1344- y
1345- }
1345+ if x.len() > y.len() { x } else { y }
13461346}
13471347```
13481348
@@ -1431,11 +1431,7 @@ src/main.rs
14311431
14321432```
14331433fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
1434- if x.len() > y.len() {
1435- x
1436- } else {
1437- y
1438- }
1434+ if x.len() > y.len() { x } else { y }
14391435}
14401436```
14411437
@@ -1735,8 +1731,8 @@ fits these cases, you don’t need to write the lifetimes explicitly.
17351731The elision rules don’t provide full inference. If there is still ambiguity
17361732about what lifetimes the references have after Rust applies the rules, the
17371733compiler won’t guess what the lifetime of the remaining references should be.
1738- Instead of guessing, the compiler will give you an error that you can resolve
1739- by adding the lifetime annotations.
1734+ Instead of guessing, the compiler will give you an error that you can resolve by
1735+ adding the lifetime annotations.
17401736
17411737Lifetimes on function or method parameters are called * input lifetimes* , and
17421738lifetimes on return values are called * output lifetimes* .
@@ -1902,11 +1898,7 @@ where
19021898 T: Display,
19031899{
19041900 println!("Announcement! {ann}");
1905- if x.len() > y.len() {
1906- x
1907- } else {
1908- y
1909- }
1901+ if x.len() > y.len() { x } else { y }
19101902}
19111903```
19121904
0 commit comments