Skip to content

Stabilize VecDeque::truncate_front#151379

Open
cammeresi wants to merge 1 commit intorust-lang:mainfrom
cammeresi:20260119-trunc
Open

Stabilize VecDeque::truncate_front#151379
cammeresi wants to merge 1 commit intorust-lang:mainfrom
cammeresi:20260119-trunc

Conversation

@cammeresi
Copy link
Copy Markdown
Contributor

@cammeresi cammeresi commented Jan 19, 2026

Tracking issue: #140667
Closes: #140667

@rustbot label -T-libs +T-libs-api +needs-fcp +S-waiting-on-fcp

r? t-libs-api

@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. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 19, 2026
@cammeresi
Copy link
Copy Markdown
Contributor Author

@BurntSushi I know process is required before this patch can be approved but is a pull request the correct way to do it or is there another way?

@cammeresi
Copy link
Copy Markdown
Contributor Author

@rustbot reroll

@Mark-Simulacrum Mark-Simulacrum added I-libs-api-nominated Nominated for discussion during a libs-api team meeting. and removed 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 Mar 22, 2026
@Amanieu
Copy link
Copy Markdown
Member

Amanieu commented Mar 24, 2026

@rfcbot merge libs-api

@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Mar 24, 2026
@rust-rfcbot
Copy link
Copy Markdown
Collaborator

rust-rfcbot commented Mar 24, 2026

Team member @Amanieu 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!

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. labels Mar 24, 2026
@dtolnay
Copy link
Copy Markdown
Member

dtolnay commented Mar 24, 2026

Some alternatives to consider:

  1. Add no method, change truncate's doc to indicate that while you can only use truncate to retain front elements, you can retain the back elements by using vecdeque.drain(..idx). Prior art: for slices we recently chose to expose front truncation using slice.split_off(..idx) which takes a OneSidedRange.

  2. Instead of putting this in the truncate family, put it in the retain family with a name like vecdeque.retain_last(N). This eliminates ambiguity about whether the argument for truncate is supposed to be an index vs a number of elements to retain, which are 2 correct mental models that different people may have for truncate, one of which does not translate correctly to truncate_front.

I would be okay with any of the 3 designs.
@rfcbot reviewed

@joshtriplett
Copy link
Copy Markdown
Member

After further consideration, I think I'd be fine shipping this as-is. I think the "retain" family isn't as good a fit; that family generally involves a predicate.

I do find the idea of steering people towards something range-based rather than truncate somewhat appealing; there's something nice about having people say "this is the range I want". But that isn't the ideal API for Vec, for instance, where anything other than ..end or ..=last would be inefficient and require a memory move; truncate makes more sense there. And for people familiar with truncate, it's nice for VecDeque to provide both truncate and truncate_front.

On that basis, I think it makes sense to stabilize this. And in addition, I'd personally love to see an API for "keep only this specified range", rather than adding any kind of truncate_both.

@rfcbot reviewed

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Apr 28, 2026
@rust-rfcbot
Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@cammeresi
Copy link
Copy Markdown
Contributor Author

@joshtriplett The idea of retaining only an inner range is an interesting idea. Do you have any example uses in mind?

The best I could imagine is collecting metrics, sorting by a field, then discarding some of the top and bottom as unrepresentative. But the sort requires going through make_contiguous currently, which makes the example unlikely to exercise all possible code paths and so a little contrived, though one could imagine a future wherein VecDeque could sort itself directly without having to coördinate through make_contiguous and slice.

@urben1680
Copy link
Copy Markdown

In a bevy crate I have rather complex deques that contain data for undo-redo actions. The collection also contains an index that defines which portion of it is the "future" segment. For example if I do not push but do undo, I get a reference of the value at index and the index is reduced. So the range after that index is considered the "future", the other elements the "past". When updating the deque with push, I also provide a maximum past length. And there I basically retain only an inner range: the future segment and any more elements of the past end that exceeds the maximum length have to go.

Though my case is even more complex because I also provide draining iterators of the past and future at this method, which I do by draining all of the deque, collect into a boxed slice if the iterator gets to the segment I want to keep, and on Drop reinsert them + the newly pushed value into the deque.

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

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking Issue for VecDeque::truncate_front

9 participants