-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add remove_item to Vecs #38143
Add remove_item to Vecs #38143
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/libcollections/vec.rs
Outdated
@@ -1296,6 +1296,17 @@ impl<T: PartialEq> Vec<T> { | |||
pub fn dedup(&mut self) { | |||
self.dedup_by(|a, b| a == b) | |||
} | |||
|
|||
#[stable(feature = "rust1", since = "1.0.0")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! This would be introduced as an unstable feature if introduced. This should be an unstable
tag, and you specify a new feature name that you invent for feature=
. See dedup_by_key
in the same file for an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will do. I wasn't entirely sure what to put for feature :P
src/libcollections/vec.rs
Outdated
None => return None, | ||
}; | ||
|
||
self.remove(pos); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps return type should be Option<(usize, T)>
, returning Some((pos, self.remove(pos)))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you return both pos
and self.remove(pos)
? Would they not be the same thing, an index to the item to be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.remove(pos) returns the element
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, ok. I was under the impression that remove returned the index to the item removed. Option<(usize, T)>
seems like a good idea to me.
src/libcollections/vec.rs
Outdated
@@ -1296,6 +1296,17 @@ impl<T: PartialEq> Vec<T> { | |||
pub fn dedup(&mut self) { | |||
self.dedup_by(|a, b| a == b) | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line has trailing whitespace which will need to be removed, that's why travis failed
/cc @rust-lang/libs |
src/libcollections/vec.rs
Outdated
@@ -1296,6 +1296,17 @@ impl<T: PartialEq> Vec<T> { | |||
pub fn dedup(&mut self) { | |||
self.dedup_by(|a, b| a == b) | |||
} | |||
|
|||
#[unstable(feature = "remove_from", reason = "recently added", issue = "38143")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this feature be called "remove_item"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe even "vec_remove_item"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be, I'll call it vec_remove_item
In what context would a user want to know the index from which the value was removed? Similar methods in Java and Python do not return the index AFAIKT. |
@sfackler I've been trying to come up with a use case and I've found none. Perhaps |
That's what I would vote for, yeah. It might also be helpful to make the item argument generic over |
I'm tentatively in favor of this API, with a couple of changes:
|
|
@sfackler Oh whoops, of course. In that case, |
Changed return value to |
I've restarted Travis as it's not clear to me that the failure was legit. |
@rfcbot fcp merge |
@rfcbot fcp merge |
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, 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. |
It seems almost certain if we add a function to remove a value by comparison to another concrete value, that we will eventually also be asked to add |
@brson seems reasonable to me, yes. |
My only concern here is to do with |
@cristicbz That's possible, though we should keep in mind that these are all conveniences (and so we don't have to offer every possible combination). That said, in my experience there's relatively little cost in adding something that follows a well-known convention, because you immediately know what it does from the name. |
src/libcollections/vec.rs
Outdated
@@ -1296,6 +1296,16 @@ impl<T: PartialEq> Vec<T> { | |||
pub fn dedup(&mut self) { | |||
self.dedup_by(|a, b| a == b) | |||
} | |||
|
|||
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "38143")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have a doc comment clarifying that it removes the first item found.
|
@clarcharr
Agreed.
Unclear to me. This is one problem with convenience methods: it's not clear where to draw the line. I'm not opposed, though.
I think |
Ping @brson |
@madseagames in preparation for merging, could you be sure to document this method as well? We also tend to not require full consensus before merging new unstable APIs, so if you want to ping me when updated I'll r+! |
@madseagames oh also can you file a tracking issue and update the reference? If you cc me on the issue I'll be sure to tag it appropriately |
💔 Test failed - status-appveyor |
@bors: retry
* network error
…On Thu, Feb 23, 2017 at 8:17 PM, bors ***@***.***> wrote:
💔 Test failed - status-appveyor
<https://ci.appveyor.com/project/rust-lang/rust/build/1.0.2079>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#38143 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95EtfRjrdMOPKbq-WE_dyBeYhl-Znks5rfj2kgaJpZM4LDW6p>
.
|
⌛ Testing commit e86846d with merge 0bea301... |
💔 Test failed - status-travis |
@bors: retry
|
@bors r- Doc test needs feature-gate too. |
@alexcrichton Whats going on with Travis? Anything I need to change? Also, when will we be seeing remove_item in the rust std libs? Will it be part of the nightly or beta? |
@madseagames The example in the documentation needs the feature gate too. |
@madseagames oh the example here needs the same feature name as the method itself (as evidenced by the Travis error) |
Thanks @madseagames! Could you also squash the commits down into one as well? |
src/libcollections/vec.rs
Outdated
@@ -1165,6 +1165,15 @@ impl<T> Vec<T> { | |||
} | |||
other | |||
} | |||
|
|||
pub fn remove_item<T: PartialEq>(&mut self, item: T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I'm having some issues with github at the moment, Im working on it, I'm an absolute newbie to github and I'm trying to fix things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend trying out git gui
, especially in amend mode, and then force-pushing.
@madseagames You should still squash ( |
@eddyb So far I've been doing everything through the github website, is there a way to squash through the site? Or do I need to download the command line tool? |
@madseagames Ohhhh that explains things. Well, I'm surprised you got this far, the web UI is unusable for anything more than tiny changes and a single commit. I think I can fix it for you though. |
@eddyb How would you go about doing that? Now that you mention it I do have two definitions... |
@madseagames if you use Windows or OS X, you should be able to download GitHub's git UI that should let you squash pull requests. |
Uhm, this is very surprising. |
@madseagames I tried to use a new GitHub feature but it emptied&closed the pull request instead. EDIT: Aaaaand let it be known I suck at git, I used the wrong source branch, see #40325. |
Haha, glad to know I'm not the only one that sucks at git :P also @clarcharr I run Linux. I tried to learn git once but figured that taking so much time to learn such a complex tool wasn't worth it. |
@madseagames Oh, all of this could've been avoided, when you said "download the command-line tool" I thought you were on Windows where it's actually some work 😆 (if you want to learn, again, |
@eddyb so... Are things fixed? I'm still not entirely sure. I removed the second definition, did you squash the commits? |
@madseagames This PR is broken permanently AFAICT, had to open #40325 (which has 1 commit). |
@eddyb what do I need to do at my end? |
@madseagames Hopefully nothing, if #40325 merges. |
Added remove_from to vec.rs (rust-lang#38143) Turns out that if you push to someone's PR branch and cause the PR to close, you lose delegation 😞. @madseagames I'm really sorry about that 😭
Added remove_from to vec.rs (rust-lang#38143) Turns out that if you push to someone's PR branch and cause the PR to close, you lose delegation 😞. @madseagames I'm really sorry about that 😭
Added remove_from to vec.rs (rust-lang#38143) Turns out that if you push to someone's PR branch and cause the PR to close, you lose delegation 😞. @madseagames I'm really sorry about that 😭
Alright, so if I'm doing anything wrong please let me know. This is my first time contributing to an open source project, and I am very excited by this opportunity.
I would like to propose an addition to the standard libs. I would like to add the method
remove_item
to vecs. Why?Simplicity - removing an item from a vec is overly complicated. Consider the following code:
A new user of rust needs to know about closures, unwrapping values, iterators and two methods on vecs to simply remove an item. When I was new to rust this caused me many problems.