-
Notifications
You must be signed in to change notification settings - Fork 2
lesson 05: change text #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| ``` | ||
|
|
||
| This eliminates the heap allocations, but there's another catch. What if `NewsArticle` already had another (non-summarizing) `Display` implementation? We would end up in a double-trait-implementation conflict, which is a compile-time error. | ||
| we can see that we do one unnecessary heap allocation that we can optimize. Instead of creating a `String` (which does the heap allocation) that we then pass to `format!`, we can just use `format!` directly: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave the previous wording here as well, as it conveys the message about avoiding resource waste more clearly:
The
Summarytrait was really wasteful: it always allocated theStrings on heap, even though we only needed to display the formatted string, and we could do that without allocations.
| ## Associated types vs `impl Trait` | ||
|
|
||
| The use of generic types in `Summary` trait makes it semantics like this: | ||
| Rust offers two complementary tools for expressing "some type that implements a trait": | ||
|
|
||
| > A type can be summarized with any type supporting it. | ||
| - **Associated types** belong to a trait definition. Each implementer fills in the concrete type once, driving a consistent contract across all of the trait's methods. Use them when the trait itself needs to name a type (for example, an iterator's `Item` or a parser's `Output`). They shine when multiple methods must agree on the exact type, or when a caller should be able to refer to it explicitly (`MyIterator::Item`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before, this section would compare generic traits vs traits with associated types. Now you compare traits with explicitly specified associated types vs impl Trait with implicit associated types. This is entirely a different comparison, and I believe the comparison with generic traits should also be here.
Co-authored-by: Wojciech Przytuła <wp418383@students.mimuw.edu.pl>
Co-authored-by: Wojciech Przytuła <wp418383@students.mimuw.edu.pl>
No description provided.