Skip to content

Commit 1f76e08

Browse files
cod10129gitbot
authored and
gitbot
committed
Add another Vec::splice example
Add an example for using splice to insert multiple elements efficiently into a vector.
1 parent b2846f9 commit 1f76e08

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

alloc/src/vec/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -3587,7 +3587,7 @@ impl<T, A: Allocator> Vec<T, A> {
35873587
/// with the given `replace_with` iterator and yields the removed items.
35883588
/// `replace_with` does not need to be the same length as `range`.
35893589
///
3590-
/// `range` is removed even if the iterator is not consumed until the end.
3590+
/// `range` is removed even if the `Splice` iterator is not consumed before it is dropped.
35913591
///
35923592
/// It is unspecified how many elements are removed from the vector
35933593
/// if the `Splice` value is leaked.
@@ -3613,8 +3613,18 @@ impl<T, A: Allocator> Vec<T, A> {
36133613
/// let mut v = vec![1, 2, 3, 4];
36143614
/// let new = [7, 8, 9];
36153615
/// let u: Vec<_> = v.splice(1..3, new).collect();
3616-
/// assert_eq!(v, &[1, 7, 8, 9, 4]);
3617-
/// assert_eq!(u, &[2, 3]);
3616+
/// assert_eq!(v, [1, 7, 8, 9, 4]);
3617+
/// assert_eq!(u, [2, 3]);
3618+
/// ```
3619+
///
3620+
/// Using `splice` to insert new items into a vector efficiently at a specific position
3621+
/// indicated by an empty range:
3622+
///
3623+
/// ```
3624+
/// let mut v = vec![1, 5];
3625+
/// let new = [2, 3, 4];
3626+
/// v.splice(1..1, new);
3627+
/// assert_eq!(v, [1, 2, 3, 4, 5]);
36183628
/// ```
36193629
#[cfg(not(no_global_oom_handling))]
36203630
#[inline]

0 commit comments

Comments
 (0)