Skip to content

Commit 783062d

Browse files
committed
Auto merge of #154095 - joboet:vec_clear_truncate, r=nnethercote
std: implement `clear` via `truncate` This gets rid of some `unsafe`. `truncate(0)` is even documented to be equivalent to `clear`, this makes that equivalence even more obvious.
2 parents a51f3a8 + f8b83b2 commit 783062d

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,10 @@ impl<T, A: Allocator> Vec<T, A> {
29922992
#[inline]
29932993
#[stable(feature = "rust1", since = "1.0.0")]
29942994
pub fn clear(&mut self) {
2995+
// Though this is equivalent to `truncate(0)`, the manual version
2996+
// optimizes better, justifying the additional complexity
2997+
// (see #96002 and #154095 for context).
2998+
29952999
let elems: *mut [T] = self.as_mut_slice();
29963000

29973001
// SAFETY:

0 commit comments

Comments
 (0)