Skip to content

Commit 740921a

Browse files
authored
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.
2 parents 36176b5 + 3b71cc8 commit 740921a

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

alloc/src/vec/into_iter.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
258258
self.len()
259259
}
260260

261+
#[inline]
262+
fn last(mut self) -> Option<T> {
263+
self.next_back()
264+
}
265+
261266
#[inline]
262267
fn next_chunk<const N: usize>(&mut self) -> Result<[T; N], core::array::IntoIter<T, N>> {
263268
let mut raw_ary = [const { MaybeUninit::uninit() }; N];

std/src/sys/args/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl Iterator for Args {
4949
}
5050

5151
#[inline]
52-
fn last(mut self) -> Option<OsString> {
53-
self.iter.next_back()
52+
fn last(self) -> Option<OsString> {
53+
self.iter.last()
5454
}
5555

5656
#[inline]

0 commit comments

Comments
 (0)