-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement Iterator::last
for vec::IntoIter
#139773
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
rustbot has assigned @workingjubilee. Use |
This comment has been minimized.
This comment has been minimized.
ee2370d
to
542bbe7
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
I'm unsatisfied that this suppresses the Clippy diagnostic for |
This comment has been minimized.
This comment has been minimized.
542bbe7
to
88b7959
Compare
This comment has been minimized.
This comment has been minimized.
88b7959
to
e6d7a27
Compare
This removes the whole purpose of the test which is to check whether this would modify the drop order of the collection's elements. Could you maybe replace the |
I think making a custom Do you think it's fine that this case is no longer linted for |
I'm confused why you edited my comment. Perhaps you were grabbing the Markdown contents for a reply and edited it instead? I've reverted the edit and reproduce your comment below: @samueltardieu commented 2025-04-14T14:02:22Z:
Independently from the Clippy lint, I wonder if your
For me, it implies that elements are dropped in order. So I'd say that if your change is accepted (and the documentation for |
e6d7a27
to
978a3cb
Compare
I've now made a little wrapper iterator for the test to still exercise that same case, since it wasn't really intending to test the implementation details of |
Ooops, this is was not only unintentional but also unnoticed. Thanks for fixing this. |
☔ The latest upstream changes (presumably #139983) made this pull request unmergeable. Please resolve the merge conflicts. |
978a3cb
to
e9fd1b2
Compare
The semantics of
Note the "consumes" part. If I understand this change correctly, on some Vecs (depending on exact size) I can now call |
I am not convinced that this implementation is not reneging on I think it can probably be fixed to have this obviously-faster implementation while still upholding the equivalent of that contract? But I am not sure. I am not going to think that far right now. This either
I will leave it to your judgement to decide which path is viable. @rustbot author |
Reminder, once the PR becomes ready for a review, use |
I think the detail you're missing is that |
squint Yep, that's probably why Jubilee doesn't know what she is talking about. Let me take a closer look. I have a question about a particular case involving iterators that have side effects upon iteration that I should just write the test for... |
Oh, by the way, if #139847 merges first, I'll add this bit here, though it's entirely inconsequential. It was actually how I discovered this. --- a/library/std/src/sys/args/common.rs
+++ b/library/std/src/sys/args/common.rs
@@ -51,4 +51,4 @@ impl Iterator for Args {
#[inline]
- fn last(mut self) -> Option<OsString> {
- self.iter.next_back()
+ fn last(self) -> Option<OsString> {
+ self.iter.last()
} |
The drop order of This is the current status quo: struct D(u32);
impl Drop for D {
fn drop(&mut self) {
println!("drop {}", self.0);
}
}
println!("Vec::drop");
drop(vec![D(1), D(2), D(3)]);
println!("vec::IntoIter::last");
drop(vec![D(4), D(5), D(6)].into_iter().last());
|
still cogitating but first @rustbot ready |
e9fd1b2
to
23c14a5
Compare
23c14a5
to
cbdd713
Compare
I've applied the patch I mentioned, now that #139847 has merged. |
I... feel very skeptical of the standard library's use of specialization, now, as it contributed greatly to my initial uncertainty. I feel like it weakens the ability to do generic reasoning. However, I am, for now, reasonably confident that it does not affect this PR. @bors r+ |
…workingjubilee Implement `Iterator::last` for `vec::IntoIter` Avoid iterating everything when we have random access to the last element.
Rollup of 12 pull requests Successful merges: - rust-lang#139550 (Fix `-Zremap-path-scope` rmeta handling) - rust-lang#139773 (Implement `Iterator::last` for `vec::IntoIter`) - rust-lang#140035 (Implement RFC 3503: frontmatters) - rust-lang#140176 (Fix linking statics on Arm64EC) - rust-lang#140251 (coverage-dump: Resolve global file IDs to filenames) - rust-lang#140393 (std: get rid of `sys_common::process`) - rust-lang#140532 (Fix RustAnalyzer discovery of rustc's `stable_mir` crate) - rust-lang#140598 (Steer docs to `utf8_chunks` and `Iterator::take`) - rust-lang#140634 (Use more accurate ELF flags on MIPS) - rust-lang#140673 (Clean rustdoc tests folder) - rust-lang#140678 (Be a bit more relaxed about not yet constrained infer vars in closure upvar analysis) - rust-lang#140687 (Update mdbook to 0.4.49) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 12 pull requests Successful merges: - rust-lang#139550 (Fix `-Zremap-path-scope` rmeta handling) - rust-lang#139764 (Consistent trait bounds for ExtractIf Debug impls) - rust-lang#139773 (Implement `Iterator::last` for `vec::IntoIter`) - rust-lang#140035 (Implement RFC 3503: frontmatters) - rust-lang#140251 (coverage-dump: Resolve global file IDs to filenames) - rust-lang#140393 (std: get rid of `sys_common::process`) - rust-lang#140532 (Fix RustAnalyzer discovery of rustc's `stable_mir` crate) - rust-lang#140598 (Steer docs to `utf8_chunks` and `Iterator::take`) - rust-lang#140634 (Use more accurate ELF flags on MIPS) - rust-lang#140673 (Clean rustdoc tests folder) - rust-lang#140678 (Be a bit more relaxed about not yet constrained infer vars in closure upvar analysis) - rust-lang#140687 (Update mdbook to 0.4.49) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#139773 - thaliaarchi:vec-into-iter-last, r=workingjubilee Implement `Iterator::last` for `vec::IntoIter` Avoid iterating everything when we have random access to the last element.
Avoid iterating everything when we have random access to the last element.