Skip to content

Commit

Permalink
Rollup merge of #133790 - HypheX:improve-vec-docs, r=harudagondi,work…
Browse files Browse the repository at this point in the history
…ingjubilee

Improve documentation for Vec::extend_from_within

This closes #104762.

It rephrases some of the explanations, and greatly improves the clarity of the example.
Based on this PR and its discussions:
https://github.com/rust-lang/rust/pull/105030/files#r1059808792
  • Loading branch information
matthiaskrgr authored Dec 6, 2024
2 parents 576176d + b781165 commit b780404
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3025,26 +3025,29 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
self.spec_extend(other.iter())
}

/// Copies elements from `src` range to the end of the vector.
/// Given a range `src`, clones a slice of elements in that range and appends it to the end.
///
/// `src` must be a range that can form a valid subslice of the `Vec`.
///
/// # Panics
///
/// Panics if the starting point is greater than the end point or if
/// the end point is greater than the length of the vector.
/// Panics if starting index is greater than the end index
/// or if the index is greater than the length of the vector.
///
/// # Examples
///
/// ```
/// let mut vec = vec![0, 1, 2, 3, 4];
///
/// vec.extend_from_within(2..);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
/// let mut characters = vec!['a', 'b', 'c', 'd', 'e'];
/// characters.extend_from_within(2..);
/// assert_eq!(characters, ['a', 'b', 'c', 'd', 'e', 'c', 'd', 'e']);
///
/// vec.extend_from_within(..2);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
/// let mut numbers = vec![0, 1, 2, 3, 4];
/// numbers.extend_from_within(..2);
/// assert_eq!(numbers, [0, 1, 2, 3, 4, 0, 1]);
///
/// vec.extend_from_within(4..8);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
/// let mut strings = vec![String::from("hello"), String::from("world"), String::from("!")];
/// strings.extend_from_within(1..=2);
/// assert_eq!(strings, ["hello", "world", "!", "world", "!"]);
/// ```
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "vec_extend_from_within", since = "1.53.0")]
Expand Down

0 comments on commit b780404

Please sign in to comment.