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?).

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>` I-libs-nominated Nominated for discussion during a libs team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. 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.

7 participants