Skip to content

Commit 0c9b35b

Browse files
authored
Rollup merge of #87644 - Flying-Toast:vec-remove-note, r=the8472
Recommend `swap_remove` in `Vec::remove` docs I was able to increase the performance (by 20%!) of my project by changing a `Vec::remove` call to `Vec::swap_remove` in a hot function. I think we should explicitly put a note in the Vec::remove docs to guide people in the right direction so they don't make a similar oversight.
2 parents e924e76 + 9a2e3f3 commit 0c9b35b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

library/alloc/src/vec/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,12 @@ impl<T, A: Allocator> Vec<T, A> {
13601360
/// Removes and returns the element at position `index` within the vector,
13611361
/// shifting all elements after it to the left.
13621362
///
1363+
/// Note: Because this shifts over the remaining elements, it has a
1364+
/// worst-case performance of O(n). If you don't need the order of elements
1365+
/// to be preserved, use [`swap_remove`] instead.
1366+
///
1367+
/// [`swap_remove`]: Vec::swap_remove
1368+
///
13631369
/// # Panics
13641370
///
13651371
/// Panics if `index` is out of bounds.

0 commit comments

Comments
 (0)