about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcod10129 <110200933+cod10129@users.noreply.github.com>2025-01-11 11:39:40 -0600
committercod10129 <110200933+cod10129@users.noreply.github.com>2025-01-13 17:37:09 -0600
commitb11f87a53cb3405eafc6af02ce41b4ef2756c174 (patch)
tree543f0e51ba7ac671a9885e4e7b1c33d0e02f2b24
parentfb65a3ee576feab95a632eb062f466d7a0342310 (diff)
downloadrust-b11f87a53cb3405eafc6af02ce41b4ef2756c174.tar.gz
rust-b11f87a53cb3405eafc6af02ce41b4ef2756c174.zip
Add another `Vec::splice` example
Add an example for using splice to insert multiple elements efficiently into a vector.
-rw-r--r--library/alloc/src/vec/mod.rs16
1 files changed, 13 insertions, 3 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs
index 3a706d5f36b..cd2afd7a473 100644
--- a/library/alloc/src/vec/mod.rs
+++ b/library/alloc/src/vec/mod.rs
@@ -3587,7 +3587,7 @@ impl<T, A: Allocator> Vec<T, A> {
     /// with the given `replace_with` iterator and yields the removed items.
     /// `replace_with` does not need to be the same length as `range`.
     ///
-    /// `range` is removed even if the iterator is not consumed until the end.
+    /// `range` is removed even if the `Splice` iterator is not consumed before it is dropped.
     ///
     /// It is unspecified how many elements are removed from the vector
     /// if the `Splice` value is leaked.
@@ -3613,8 +3613,18 @@ impl<T, A: Allocator> Vec<T, A> {
     /// let mut v = vec![1, 2, 3, 4];
     /// let new = [7, 8, 9];
     /// let u: Vec<_> = v.splice(1..3, new).collect();
-    /// assert_eq!(v, &[1, 7, 8, 9, 4]);
-    /// assert_eq!(u, &[2, 3]);
+    /// assert_eq!(v, [1, 7, 8, 9, 4]);
+    /// assert_eq!(u, [2, 3]);
+    /// ```
+    ///
+    /// Using `splice` to insert new items into a vector efficiently at a specific position
+    /// indicated by an empty range:
+    ///
+    /// ```
+    /// let mut v = vec![1, 5];
+    /// let new = [2, 3, 4];
+    /// v.splice(1..1, new);
+    /// assert_eq!(v, [1, 2, 3, 4, 5]);
     /// ```
     #[cfg(not(no_global_oom_handling))]
     #[inline]