Skip to content

Conversation

@tonowak
Copy link
Contributor

@tonowak tonowak commented Oct 20, 2025

No description provided.

@tonowak tonowak requested a review from wprzytula October 20, 2025 22:43
```

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:
Copy link
Contributor

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 Summary trait was really wasteful: it always allocated the Strings on heap, even though we only needed to display the formatted string, and we could do that without allocations.

Comment on lines 363 to 367
## 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`).
Copy link
Contributor

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.

@tonowak tonowak requested a review from wprzytula October 22, 2025 19:58
@tonowak tonowak merged commit 86431c2 into main Oct 22, 2025
2 checks passed
@tonowak tonowak deleted the lessons-05-change-text branch October 22, 2025 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants