Skip to content

Commit 542bbe7

Browse files
committed
Implement Iterator::last for vec::IntoIter
1 parent 092a284 commit 542bbe7

File tree

3 files changed

+7
-30
lines changed

3 files changed

+7
-30
lines changed

Diff for: library/alloc/src/vec/into_iter.rs

+5
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];

Diff for: src/tools/clippy/tests/ui/double_ended_iterator_last.stderr

+1-14
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,5 @@ LL ~ let (mut subindex, _) = (index.by_ref().take(3), 42);
6565
LL ~ let _ = subindex.next_back();
6666
|
6767

68-
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
69-
--> tests/ui/double_ended_iterator_last.rs:89:36
70-
|
71-
LL | println!("Last element is {}", v.last().unwrap().0);
72-
| ^^^^^^^^
73-
|
74-
= note: this change will alter drop order which may be undesirable
75-
help: try
76-
|
77-
LL ~ let mut v = v.into_iter();
78-
LL ~ println!("Last element is {}", v.next_back().unwrap().0);
79-
|
80-
81-
error: aborting due to 8 previous errors
68+
error: aborting due to 7 previous errors
8269

Diff for: src/tools/clippy/tests/ui/double_ended_iterator_last_unfixable.stderr

+1-16
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,5 @@ LL | let _ = subindex.0.last();
1414
= note: `-D clippy::double-ended-iterator-last` implied by `-D warnings`
1515
= help: to override `-D warnings` add `#[allow(clippy::double_ended_iterator_last)]`
1616

17-
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
18-
--> tests/ui/double_ended_iterator_last_unfixable.rs:20:36
19-
|
20-
LL | println!("Last element is {}", v.0.last().unwrap().0);
21-
| ^^^^------
22-
| |
23-
| help: try: `next_back()`
24-
|
25-
= note: this change will alter drop order which may be undesirable
26-
note: this must be made mutable to use `.next_back()`
27-
--> tests/ui/double_ended_iterator_last_unfixable.rs:20:36
28-
|
29-
LL | println!("Last element is {}", v.0.last().unwrap().0);
30-
| ^^^
31-
32-
error: aborting due to 2 previous errors
17+
error: aborting due to 1 previous error
3318

0 commit comments

Comments
 (0)