Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for Vec::remove_item #40062

Closed
madseagames opened this issue Feb 23, 2017 · 72 comments · Fixed by #67727 or #80972
Closed

Tracking issue for Vec::remove_item #40062

madseagames opened this issue Feb 23, 2017 · 72 comments · Fixed by #67727 or #80972
Labels
A-collections Area: `std::collection` B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. finished-final-comment-period The final comment period is finished for this PR / Issue. Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@madseagames
Copy link
Contributor

madseagames commented Feb 23, 2017

Implemented in #40325

impl<T: PartialEq> Vec<T> {
    pub fn remove_item(&mut self, item: &T) -> Option<T> { /*…*/ }
}
@alexcrichton alexcrichton added B-unstable Blocker: Implemented in the nightly compiler and unstable. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Feb 23, 2017
@clarfonthey
Copy link
Contributor

PR moved to #40325 due to reasons

@clarfonthey
Copy link
Contributor

Comments from original thread:

  1. I think that we should add this method to VecDeque too.
  2. Is remove_last_item desired?

@madseagames
Copy link
Contributor Author

@clarcharr for remove_last_item you can simply do vec.remove(vec.len() - 1))

@clarfonthey
Copy link
Contributor

@madseagames I meant specifically a method that searches for the item from the end of the vec instead of the beginning

@oberien
Copy link
Contributor

oberien commented Mar 15, 2017

What do you think about adding remove_swap_item? Just as remove_swap reduces the runtime complexity from O(n) to O(1) compared to remove, remove_swap_item would reduce it from O(n + (n-m)) to O(n).

@clarfonthey
Copy link
Contributor

I like swap_remove_item.

@MarkSwanson
Copy link

It would be nice if this was more generic.
let v: Vec = Vec::new();
v.remove_item("s"); <-- fails because &str is not a &String

we really want the remove_item "item: &T" to be "something that we can compare equality with the type in the container". This would let remove_item() walk the T items, compare equality, and remove if equal.

@sfackler
Copy link
Member

sfackler commented Jun 3, 2017

Seems reasonable to change the signature to fn remove_item<U>(&mut self, item: &U) -> Option<T> where T: PartialEq<U> + ?Sized

@Mark-Simulacrum Mark-Simulacrum added the C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. label Jul 22, 2017
@leoyvens
Copy link
Contributor

Reviewing the naming used in method names and the docs, it would seem that iterators yield items while vecs contain elements. Considering that we insert elements then we should also remove elements and the method should be called remove_element. That name is a bit long so I'd propose simply remove_eq. However I wonder if we should favor a more general remove_by, or have both.

fn remove_by<F: FnMut(&T) -> bool>(&mut self, f: F)

@zbraniecki
Copy link

Any plan to stabilize this?

@SimonSapin
Copy link
Contributor

This has been in nightly for a year with no issue reported on the method itself. There are proposals for additional methods, but that shouldn’t block this one. Please submit separate PRs or RFCs.

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Mar 17, 2018

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

Concerns:

Once a majority of reviewers approve (and none object), 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.

@rfcbot rfcbot added the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Mar 17, 2018
@sfackler
Copy link
Member

Are we switching to a V: PartialEq<T> bound?

@SimonSapin
Copy link
Contributor

Oops, sorry I missed that. Yes, let’s.

@gnzlbg
Copy link
Contributor

gnzlbg commented Mar 18, 2018

Again here, as a maintainer of collections that offer the Vec and VecDeque APIs I'd like at least a summary comment explaining what this method does, why this design was followed, alternatives, and addressing the issues mentioned here that have received zero replies:

  • should this method be added to VecDeque?
  • @sfackler :

Seems reasonable to change the signature to fn remove_item<U>(&mut self, item: &U) -> Option<T> where T: PartialEq<U> + ?Sized

  • @leodasvacas naming issue: why items instead of elements? why remove_items instead of remove_by, etc.

@alexcrichton
Copy link
Member

@rfcbot concern rationale-and-vecdeque

Just gonna formally register @gnzlbg's concern above

@kornelski
Copy link
Contributor

kornelski commented Apr 11, 2018

Not an issue with the method per se, but it may need to be very clearly documented that the reference is not supposed to be to an item that is in the Vec already, as it creates impossible borrow-checker conundrum.

Users coming from the C world of pointers but no proper equality comparison, may be confused by this method, e.g. https://users.rust-lang.org/t/difficult-borrowing-situation/16794

The remove_eq name mentioned previously would help.

@alexcrichton
Copy link
Member

@SimonSapin as the proposer for FCP, do you have thoughts on @gnzlbg's concern?

@Centril Centril added the disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. label May 24, 2018
@droundy
Copy link
Contributor

droundy commented Sep 3, 2018

My thinking is that the remove_by suggested by @leodasvacas would sidestep most of the issues here.

@Centril
Copy link
Contributor

Centril commented Dec 29, 2018

cc @SimonSapin re. #40062 (comment)

@SimonSapin
Copy link
Contributor

I don’t have a strong desire for this particular feature myself, I proposed FCP as an effort to reduce the number of features in "unstable limbo". That said:

Regarding VecDeque, sure, I don’t see a reason not to add it there as well.

remove_by as proposed in #40062 (comment) sounds good to me. It is more general, and goes with precedent of Iterator::position also taking a boolean callable predicate rather than relying on PartialEq. (Even further, the precedent of Iterator not having a method like position that would rely on PartialEq.)

@withoutboats
Copy link
Contributor

I notice two things:

  1. There are a lot of methods similar to this we could add, and its not obvious to me which set of those methods is the ideal set.
  2. This has sat in FCP concern limbo for 8 months without anyone complaining.

I think we should not stabilize this just because it exists, but instead someone should come up with a well justified explanation of exactly what the best "find and remove" set of methods of vec-likes would be.

@SimonSapin
Copy link
Contributor

Good points.

drain (which is stable) and drain_filter #43244 also fit the "find and remove" description, and we had a similar concern #43244 (comment) about the combinations of filtering v.s. non-filtering and self-exhausting on drop (rust-lang/rfcs#2369) v.s. not.

@alexcrichton
Copy link
Member

@rfcbot resolve rationale-and-vecdeque

I don't want to personally be on the hook for blocking this any more

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Apr 15, 2020
@SimonSapin
Copy link
Contributor

Checking KodrAus’ checkbox per rust-lang/team@45013d1

@rfcbot
Copy link

rfcbot commented Apr 15, 2020

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

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Apr 15, 2020
@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label Apr 25, 2020
@rfcbot
Copy link

rfcbot commented Apr 25, 2020

The final comment period, with a disposition to close, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Apr 25, 2020
kornelski added a commit to kornelski/rust that referenced this issue May 7, 2020
RalfJung added a commit to RalfJung/rust that referenced this issue Jun 20, 2020
…e-item, r=Mark-Simulacrum

Deprecate `Vec::remove_item`

In rust-lang#40062 we decided to remove that method. In rust-lang#71834 it was said that we want to deprecate it for a few cycles before removing it. That's what this PR does.
@KodrAus
Copy link
Contributor

KodrAus commented Jul 29, 2020

Since we've merged the deprecation would we like to close this tracking issue now? Or keep it open until the unstable deprecated method is removed entirely?

@LukasKalbertodt
Copy link
Member

I think keeping this open until we removed the method is a good idea. How long do we want to wait with that by the way? I guess a couple of cycles is sufficient?

@KodrAus KodrAus added the Libs-Tracked Libs issues that are tracked on the team's project board. label Jul 31, 2020
@AgarwalPragy
Copy link

Before closing, it would be great if someone could suggest a "one clean and obvious" way to remove the first/last occurance of an item from a Vector.

Irrespective of the arguments made here, a method removing the first/last occurance of an item is provided across all major programming languages, and is to be "expected"

Anyone new to rust is bound to google "remove from vec rust" and end up on this discussion which would only leave them more confused.

@LukasKalbertodt
Copy link
Member

This StackOverflow Q&A should answer those questions. But here is a copy of my answer for convenience:


Remove first element equal to needle

// Panic if no such element is found
vec.remove(vec.iter().position(|x| *x == needle).expect("needle not found"));

// Ignore if no such element is found
if let Some(pos) = vec.iter().position(|x| *x == needle) {
    vec.remove(pos);
}

You can of course handle the None case however you like (panic and ignoring are not the only possibilities).

Remove last element equal to needle

Like the first element, but replace position with rposition.

Remove all elements equal to needle

vec.retain(|x| *x != needle);

... or with swap_remove

Remember that remove has a runtime of O(n) as all elements after the index need to be shifted. Vec::swap_remove has a runtime of O(1) as it swaps the to-be-removed element with the last one. If the order of elements is not important in your case, use swap_remove instead of remove!

@LukasKalbertodt
Copy link
Member

It might be worth adding more examples (maybe all of the ones above) to the Vec::remove docs.

@nrabulinski
Copy link
Contributor

I'd like to add those to the docs, should they go along the example that we currently have?

@LukasKalbertodt
Copy link
Member

@nrabulinski What examples are you referring to? I could imagine those "remove examples" either going on the type (as a new section "Removing elements" or something like that) or directly on the Vec::remove method (below the existing docs). And by the way, feel free to add (parts of) my text/examples verbatim.

@nrabulinski
Copy link
Contributor

Thank you. I was referring to those "remove examples" as to whether I should append them to what we currently have in docs for Vec::remove, put them somewhere else or perhaps modify examples for Vec::remove to include removal of specific element

@LukasKalbertodt
Copy link
Member

append them to what we currently have in docs for Vec::remove

That sounds fine to me. Feel free to just open a PR and we can still see about the specifics then :)

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 14, 2021
…gisa

Remove unstable deprecated Vec::remove_item

Closes rust-lang#40062

The `Vec::remove_item` method was deprecated in `1.46.0` (in August of 2020). This PR now removes that unstable method entirely.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 14, 2021
…gisa

Remove unstable deprecated Vec::remove_item

Closes rust-lang#40062

The `Vec::remove_item` method was deprecated in `1.46.0` (in August of 2020). This PR now removes that unstable method entirely.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 14, 2021
…gisa

Remove unstable deprecated Vec::remove_item

Closes rust-lang#40062

The `Vec::remove_item` method was deprecated in `1.46.0` (in August of 2020). This PR now removes that unstable method entirely.
m-ou-se added a commit to m-ou-se/rust that referenced this issue Jan 14, 2021
…gisa

Remove unstable deprecated Vec::remove_item

Closes rust-lang#40062

The `Vec::remove_item` method was deprecated in `1.46.0` (in August of 2020). This PR now removes that unstable method entirely.
@bors bors closed this as completed in 9bfe6f1 Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. finished-final-comment-period The final comment period is finished for this PR / Issue. Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.