-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add a range argument to vec.extract_if #133265
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
Conversation
This comment has been minimized.
This comment has been minimized.
/// ``` | ||
/// #![feature(extract_if)] | ||
/// let mut items = vec![0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 2, 1, 2]; | ||
/// let ones = items.extract_if(7.., |x| *x == 1).collect::<Vec<_>>(); |
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.
Maybe change one of the first few 0
s to 1
to indicate they don't get extracted (out of range)?
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.
Hrm, I wanted to show the "I have a prefix that I know doesn't contain those things, so I can skip them" use.
34ed582
to
0076504
Compare
This comment has been minimized.
This comment has been minimized.
0076504
to
d8ce784
Compare
This comment has been minimized.
This comment has been minimized.
5d0e0a7
to
aabed33
Compare
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.
LGTM.
The FCP finished -- do you want to go ahead and add the stabilization too?
r? @cuviper (sorry, I didn't realize this was assigned to me)
Since this is a pretty significant API and implementation change, maybe it would be good to hold off for a couple of weeks? In case anyone using this API unstably has feedback after the change (I doubt it, but doesn't hurt). |
Changing and stabilizing in the same PR doesn't seem ideal to me either, especially when the change happened parallel to the FCP. I think it should bake a few days at least. |
@bors r+ |
Add a range argument to vec.extract_if tracking issue: rust-lang#43244 This adds the range argument requested in rust-lang#43244 (comment)
…kingjubilee Rollup of 11 pull requests Successful merges: - rust-lang#131669 (lint: change help for pointers to dyn types in FFI) - rust-lang#133265 (Add a range argument to vec.extract_if) - rust-lang#133733 ( compiletest: show the difference between the normalized output and the actual output for lines which didn't match) - rust-lang#133955 (Pass the arch rather than full target name to windows_registry::find_tool) - rust-lang#133967 ([AIX] Pass -bnoipath when adding rust upstream dynamic crates) - rust-lang#133976 (Removed Unnecessary Spaces From RELEASES.md) - rust-lang#133980 ([AIX] Remove option "-n" from AIX "ln" command) - rust-lang#133987 (Define acronym for thread local storage) - rust-lang#133992 (Actually walk into lifetimes and attrs in `EarlyContextAndPass`) - rust-lang#133993 (Fix: typo in E0751 error explanation) - rust-lang#133996 (Move most tests for `-l` and `#[link(..)]` into `tests/ui/link-native-libs`) r? `@ghost` `@rustbot` modify labels: rollup
…kingjubilee Rollup of 11 pull requests Successful merges: - rust-lang#131669 (lint: change help for pointers to dyn types in FFI) - rust-lang#133265 (Add a range argument to vec.extract_if) - rust-lang#133733 ( compiletest: show the difference between the normalized output and the actual output for lines which didn't match) - rust-lang#133955 (Pass the arch rather than full target name to windows_registry::find_tool) - rust-lang#133967 ([AIX] Pass -bnoipath when adding rust upstream dynamic crates) - rust-lang#133976 (Removed Unnecessary Spaces From RELEASES.md) - rust-lang#133980 ([AIX] Remove option "-n" from AIX "ln" command) - rust-lang#133987 (Define acronym for thread local storage) - rust-lang#133992 (Actually walk into lifetimes and attrs in `EarlyContextAndPass`) - rust-lang#133993 (Fix: typo in E0751 error explanation) - rust-lang#133996 (Move most tests for `-l` and `#[link(..)]` into `tests/ui/link-native-libs`) r? `@ghost` `@rustbot` modify labels: rollup
…kingjubilee Rollup of 11 pull requests Successful merges: - rust-lang#131669 (lint: change help for pointers to dyn types in FFI) - rust-lang#133265 (Add a range argument to vec.extract_if) - rust-lang#133733 ( compiletest: show the difference between the normalized output and the actual output for lines which didn't match) - rust-lang#133955 (Pass the arch rather than full target name to windows_registry::find_tool) - rust-lang#133967 ([AIX] Pass -bnoipath when adding rust upstream dynamic crates) - rust-lang#133976 (Removed Unnecessary Spaces From RELEASES.md) - rust-lang#133980 ([AIX] Remove option "-n" from AIX "ln" command) - rust-lang#133987 (Define acronym for thread local storage) - rust-lang#133992 (Actually walk into lifetimes and attrs in `EarlyContextAndPass`) - rust-lang#133993 (Fix: typo in E0751 error explanation) - rust-lang#133996 (Move most tests for `-l` and `#[link(..)]` into `tests/ui/link-native-libs`) r? `@ghost` `@rustbot` modify labels: rollup
…kingjubilee Rollup of 11 pull requests Successful merges: - rust-lang#131669 (lint: change help for pointers to dyn types in FFI) - rust-lang#133265 (Add a range argument to vec.extract_if) - rust-lang#133733 ( compiletest: show the difference between the normalized output and the actual output for lines which didn't match) - rust-lang#133955 (Pass the arch rather than full target name to windows_registry::find_tool) - rust-lang#133967 ([AIX] Pass -bnoipath when adding rust upstream dynamic crates) - rust-lang#133976 (Removed Unnecessary Spaces From RELEASES.md) - rust-lang#133980 ([AIX] Remove option "-n" from AIX "ln" command) - rust-lang#133987 (Define acronym for thread local storage) - rust-lang#133992 (Actually walk into lifetimes and attrs in `EarlyContextAndPass`) - rust-lang#133993 (Fix: typo in E0751 error explanation) - rust-lang#133996 (Move most tests for `-l` and `#[link(..)]` into `tests/ui/link-native-libs`) r? `@ghost` `@rustbot` modify labels: rollup
Add a range argument to vec.extract_if tracking issue: rust-lang#43244 This adds the range argument requested in rust-lang#43244 (comment)
8f76b97
to
44790c4
Compare
Indeed, fixed. @bors r=cuviper |
Add a range argument to vec.extract_if tracking issue: rust-lang#43244 This adds the range argument requested in rust-lang#43244 (comment)
Rollup of 9 pull requests Successful merges: - rust-lang#133265 (Add a range argument to vec.extract_if) - rust-lang#134202 (Remove `rustc::existing_doc_keyword` lint) - rust-lang#134354 (Handle fndef rendering together with signature rendering) - rust-lang#134365 (Rename `rustc_mir_build::build` to `builder`) - rust-lang#134368 (Use links to edition guide for edition migrations) - rust-lang#134388 (Update books) - rust-lang#134397 (rustc_borrowck: Suggest changing `&raw const` to `&raw mut` if applicable) - rust-lang#134398 (AIX: add alignment info for test) - rust-lang#134400 (Fix some comments related to upvars handling) Failed merges: - rust-lang#134399 (Do not do if ! else, use unnegated cond and swap the branches instead) r? `@ghost` `@rustbot` modify labels: rollup
Add a range argument to vec.extract_if tracking issue: rust-lang#43244 This adds the range argument requested in rust-lang#43244 (comment)
Add a range argument to vec.extract_if tracking issue: rust-lang#43244 This adds the range argument requested in rust-lang#43244 (comment)
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#133265 (Add a range argument to vec.extract_if) - rust-lang#133801 (Promote powerpc64le-unknown-linux-musl to tier 2 with host tools) - rust-lang#134323 (coverage: Dismantle `map_data.rs` by moving its responsibilities elsewhere) - rust-lang#134378 (An octuple of polonius fact generation cleanups) - rust-lang#134408 (Regression test for RPIT inheriting lifetime from projection) - rust-lang#134423 (bootstrap: use specific-purpose ui test path for `test_valid` self-test) - rust-lang#134426 (Fix typo in uint_macros.rs) Failed merges: - rust-lang#133103 (Pass FnAbi to find_mir_or_eval_fn) r? `@ghost` `@rustbot` modify labels: rollup
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 hope this is the right place to raise this question about the sample code in this PR.
/// ``` | ||
/// # use std::cmp::min; | ||
/// # let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 6 }; | ||
/// # let mut vec = vec![1, 2, 3, 4, 5, 6]; |
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.
Apologies for the late comment, but I wanted to point out that this sample code doesn't behave the same way as the real extract_if()
. Given the same predicate and range, if your vector were [1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 6]
, then all of the 3s would be removed. i
is only incremented when an element is dropped, but range.end
is unchanged, so the items shift down. I got very confused when reading the docs and trying to square this sample code with the explanation of how the function works.
Fortunately, the real extract_if()
does not have this problem. Proposed change to the sample code:
let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 5 };
let mut vec = vec![0, 1, 2, 3, 4, 5, 6];
let range = 1..5;
let mut i = range.start;
let mut end = range.end;
while i < min(vec.len(), end) {
if some_predicate(&mut vec[i]) {
let _val = vec.remove(i);
end -= 1;
// your code here
} else {
i += 1;
}
}
assert_eq!(vec, vec![0, 1, 4, 5, 6]);
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.
If you want to propose a documentation improvement you can just open a PR.
tracking issue: #43244
This adds the range argument requested in #43244 (comment)