Skip to content

Use better generic type parameter names for Extend and FromIterator - #161379

Open
steffahn wants to merge 3 commits into
rust-lang:mainfrom
steffahn:extend_trait_param_name
Open

Use better generic type parameter names for Extend and FromIterator#161379
steffahn wants to merge 3 commits into
rust-lang:mainfrom
steffahn:extend_trait_param_name

Conversation

@steffahn

@steffahn steffahn commented Aug 20, 2026

Copy link
Copy Markdown
Member

Okay, first some context:

When talking about the Extend trait in a recent meeting, it got really confusing to talk about properties of the Extend trait for a moment because the naming convention on the trait was so weird. It's defined as Extend<A> and has a method extend<T>. But most generic implementations of it look like impl<T> Extend<T> for …iterator-type…, calling the item type T.

I think T is quite sensible for the item type here, so let's change it in the trait definition as well!

That means that <T> for the extend method must go away.. what is that anyway? Yes, it't the IntoIterator parameter that you extend with. Why isn't that just I anyway? Let's choose that! It will be more consistent with existing things like impl<I: Iterator> IntoIterator for I for instance. Or e.g. all of these… 🦀

I've then also noticed FromIterator has the same problem of calling the item A and using T: IntoIterator. Let's change that, too.

....and then of course, there are a “handful” of implementations of these methods that did just copy the signature for fn extend<T> or fn from_iter<T>, so I guess I'm fixing these as well.


TL;DR:

Change

pub trait Extend<A> {
    fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T);}

to

pub trait Extend<T> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I);}

to be more consistent with common use of T as the item type in containers, and their Extend impls; and use I as the name for the IntoIterator type.

(first commit)

then also do the basically the same thing for FromIterator<A>FromIterator<T> as well

(second commit)

Finally, adjust a few a lot of cases of these parameters in trait impls for the abovementioned traits.

(third commit)

These changes obviously don't change/break behavior of the traits and its users, but the improve the way the documentation of the traits renders (the improvement is the more sensible and more consistent names for the parameters). The only downside I'm aware of (besides that this is touching quite a few files.. though I don't feel quite as strongly about all those impls needing change as about the trait definition itself) is that this does affect some link anchor names in some cases. E.g.

- impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
+ impl<T, V: FromIterator<T>> FromIterator<Option<T>> for Option<V> {

would mean that doc.rust-lang.org/core/iter/trait.FromIterator.html#impl-FromIterator<Option<A>>-for-Option<V> no longer works, as it changes …#impl-FromIterator<Option<A>>-for-Option<V> to …#impl-FromIterator<Option<T>>-for-Option<V>

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 20, 2026
@rustbot

rustbot commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, nia-e

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@steffahn

steffahn commented Aug 20, 2026

Copy link
Copy Markdown
Member Author

This failure might be spurious, for all I can tell, so let's retry. I might as well rebase now to reduce back the number of commits (integrating the previous two fixes for test failure with the respective change they correspond to) to trigger another run ^^

Edit: Yup, it was a spurious failure, the same test passed now.

@steffahn
steffahn force-pushed the extend_trait_param_name branch from ae45a17 to 3060fe3 Compare August 20, 2026 16:57
@steffahn steffahn changed the title Improve Extend trait's generic param name some other generics, too Use better generic type parameter names for Extend and FromIterator Aug 21, 2026

@JohnTitor JohnTitor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, leaving one nits comment but r=me with or without addressing it.

View changes since this review

Comment thread library/core/src/iter/traits/collect.rs Outdated
@steffahn
steffahn force-pushed the extend_trait_param_name branch from 3060fe3 to 9bca736 Compare August 30, 2026 23:12
@steffahn

Copy link
Copy Markdown
Member Author

@bors r=JohnTitor

@rust-bors

rust-bors Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

@steffahn: 🔑 Insufficient privileges: not in review users

@JohnTitor

Copy link
Copy Markdown
Member

Oh sorry I thought you had, @bors r+

@rust-bors

rust-bors Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 9bca736 has been approved by JohnTitor

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 31, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Aug 31, 2026
…r=JohnTitor

Use better generic type parameter names for `Extend` and `FromIterator`

Change
```rs
pub trait Extend<A> {
    fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T);
    …
}
```
to
```rs
pub trait Extend<T> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I);
    …
}
```

to be more consistent with common use of `T` as the item type in containers, and their `Extend` impls; and use `I` as the name for the `IntoIterator` type.

also do the basically the same thing for `FromIterator<A>`→`FromIterator<T>` as well

cases of these parameters in trait impls for the abovementioned traits.

These changes obviously don't change/break behavior of the traits and its users, but the improve the way the documentation of the traits renders (the improvement is the more sensible and more consistent names for the parameters). The only downside I'm aware of (besides that this is touching quite a few files..) is that this does affect some link anchor names in *some* cases. E.g.
```diff
- impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
+ impl<T, V: FromIterator<T>> FromIterator<Option<T>> for Option<V> {
```
would mean that [doc.rust-lang.org/core/iter/trait.FromIterator.html#impl-FromIterator<Option\<A>>-for-Option\<V>](https://doc.rust-lang.org/core/iter/trait.FromIterator.html#impl-FromIterator%3COption%3CA%3E%3E-for-Option%3CV%3E) no longer works, as it changes `…#impl-FromIterator<Option<A>>-for-Option<V>` to `…#impl-FromIterator<Option<T>>-for-Option<V>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants