Skip to content

Stabilize Vec::repeat{_in} - #166120

Open
Gelbpunkt wants to merge 1 commit into
rust-lang:mainfrom
Gelbpunkt:stabilize-vec-repeat
Open

Gelbpunkt wants to merge 1 commit into
rust-lang:mainfrom
Gelbpunkt:stabilize-vec-repeat

Conversation

@Gelbpunkt

@Gelbpunkt Gelbpunkt commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Public API

impl <T: Clone> Vec<T, Global> {
    pub fn repeat(elem: T, n: usize) -> Self;
}

impl <T: Clone, A: Allocator> Vec<T, A> {
    pub fn repeat_in(elem: T, n: usize, alloc: A) -> Self;
}

Implementation history

These two methods were exposed very recently, but the implementation is quite old and was previously already available via the vec![elem; n] macro syntax.

Notes

The stabilization would be proposed for 1.103, not for 1.102, since the methods only got added in 1.102. But I believe this can be FCP'd already and we can leave the PR open until 1.103.

@rustbot rustbot added A-slice-vec Area: `[T]` and `Vec<T>` 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 Nov 17, 2026
@rustbot

rustbot commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
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 38 candidates
  • Random selection from ChrisDenton, JohnTitor, Mark-Simulacrum, clarfonthey, maxdexh

@folkertdev folkertdev added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. I-libs-nominated Nominated for discussion during a libs team meeting. S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Nov 17, 2026
@ChrisDenton

Copy link
Copy Markdown
Member

This seems very premature. As you say, they've only just been added on nightly. Usually we want to allow time for people to use and have experience with methods before proposing stabilisation.

@Gelbpunkt

Copy link
Copy Markdown
Contributor Author

This seems very premature. As you say, they've only just been added on nightly. Usually we want to allow time for people to use and have experience with methods before proposing stabilisation.

I've discussed these APIs with Nia and she encouraged me to propose stabilization immediately. We saw no reason to wait because we don't see how we could come to a different conclusion regarding the design of these APIs 6 months from now vs today.

@tgross35

Copy link
Copy Markdown
Member

I can't think of any reason to jump the gun here, or for any other stabilization. We're unlikely to find possible changes in 6 months, but we're pretty much giving up any chance of doing so if API addition and stabilization happen within only a few weeks of each other. And we owe it to our users to give some reasonable time to provide feedback, maybe there is something that team members haven't thought of.

@folkertdev

Copy link
Copy Markdown
Contributor

To me this falls more in the bool::toggle category of trivial building block, I really don't see what could be missed here in terms of API?

The name was agreed on in the ACP, the implementation is over a decade old and heavily exercised by vec!, in terms of signature there isn't anything you can do. For the numeric types (and anything that reduces to them) these methods expose memset on an allocation (in safe code), with a special optimization for fast zeroed allocations using calloc (which is not guaranteed but practically very valuable).

Especially Vec::repeat_in is a fundamental building block for any serious use of custom allocators. There is no safe, performant alternative today. Had this been part of the custom allocator stabilization (and in hindsight it really should have been), I don't think this function would have attracted any attention.

There is no particular reason to rush, but I also don't see a reason to wait in this case, and a significant upside in having access to these APIs earlier rather than later.

@jdahlstrom

jdahlstrom commented Nov 18, 2026 •

Copy link
Copy Markdown

Especially Vec::repeat_in is a fundamental building block for any serious use of custom allocators. There is no safe, performant alternative today.

Indeed, but I think a question worth asking is whether it should be spelled

<[_]>::repeat_in(&self, n: usize, alloc: A)

instead, complementing the already-existing <[_]>::repeat (which can do what the unstable Vec::repeat does, except requiring Copy rather than Clone but also being able to cycle an arbitrary slice, not just a single element. An interesting consequence is that:

#![feature(vec_repeat)]

let v: Vec<_> = <[_]>::repeat(&[1, 2, 3], 2);
let u: Vec<_> = Vec::repeat(&[1, 2, 3], 2);

look almost the same, but v is [1, 2, 3, 1, 2, 3] whereas u is [[1, 2, 3], [1, 2,3]]!)

I feel that the existence of <[_]>::repeat has sort of been overlooked in the proposal and discussion of this feature. Then again, the proposed Vec::repeat is sort of consistent with array::repeat and iter::repeat[_n] in being a non-method constructor function, so there's that.

@clarfonthey

Copy link
Copy Markdown
Contributor

I've discussed these APIs with Nia and she encouraged me to propose stabilization immediately. We saw no reason to wait because we don't see how we could come to a different conclusion regarding the design of these APIs 6 months from now vs today.

Just to quote Nia on Zulip, it appears that the suggestion was not immediately, but within a few releases, and I think that maybe some disconnect happened between the proposal.

Note that a release would be 6 weeks, not 6 months, and I agree that we don't need to wait a full 6 months before proposing. My inclination would be to simply close this now and reopen later if we get more feedback (noting that right now, we're still trying to get through FCP backlogs, and should at least get through those before trying to add more), but we could also leave this open and delay FCP.

Will defer to FCP folks on what we do here.

@tgross35

Copy link
Copy Markdown
Member

instead, complementing the already-existing <[_]>::repeat (which can do what the unstable Vec::repeat does, except requiring Copy rather than Clone but also being able to cycle an arbitrary slice, not just a single element. An interesting consequence is that:

For what it's worth, from the name I also expected this to be akin to the slice repeat method that takes an existing Vec and repeats its contents somehow. The from_elem name seemed to hint better that it's a constructor (perhaps from_repeated?).

@clarfonthey

Copy link
Copy Markdown
Contributor

So, meeting update: we talked a bit about this but unfortunately did not have a lot of time as there was a lot on the agenda. Trying to build broader consensus on the team on exactly why our "wait before stabilization" policy exists, alongside what exactly, concretely, we want to do about this.

In terms of the naming, unfortunately, std::array::repeat exists and we might as well be consistent with that, but also I know from_elem was decided as an alternative. We can paint this shed during FCP.

I'm going to start FCP with the caveat that we may end up delaying the final decision anyway, although hopefully soon ish we'll have a better policy written down on why/when/how we plan to delay stabilisations to ensure that we get necessary feedback.

@rfcbot merge libs

@clarfonthey clarfonthey removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Nov 24, 2026
@rust-rfcbot

rust-rfcbot commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

@clarfonthey has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. and removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-slice-vec Area: `[T]` and `Vec<T>` disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-blocked Status: Blocked on something else such as an RFC or other implementation work. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants