Skip to content

Commit 23f9b92

Browse files
committed
Add String::extend_from_within
This patch adds `String::extend_from_within` function under the `string_extend_from_within` feature gate similar to the `Vec::extend_from_within` function.
1 parent 4664725 commit 23f9b92

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#![feature(associated_type_bounds)]
145145
#![feature(slice_group_by)]
146146
#![feature(decl_macro)]
147+
#![feature(bindings_after_at)]
147148
// Allow testing this library
148149

149150
#[cfg(test)]

library/alloc/src/string.rs

+36
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,42 @@ impl String {
843843
self.vec.extend_from_slice(string.as_bytes())
844844
}
845845

846+
/// Copies elements from `src` range to the end of the string.
847+
///
848+
/// ## Panics
849+
///
850+
/// Panics if the starting point or end point do not lie on a [`char`]
851+
/// boundary, or if they're out of bounds.
852+
///
853+
/// ## Examples
854+
///
855+
/// ```
856+
/// #![feature(string_extend_from_within)]
857+
/// let mut string = String::from("abcde");
858+
///
859+
/// string.extend_from_within(2..);
860+
/// assert_eq!(string, "abcdecde");
861+
///
862+
/// string.extend_from_within(..2);
863+
/// assert_eq!(string, "abcdecdeab");
864+
///
865+
/// string.extend_from_within(4..8);
866+
/// assert_eq!(string, "abcdecdeabecde");
867+
/// ```
868+
#[cfg(not(no_global_oom_handling))]
869+
#[unstable(feature = "string_extend_from_within", issue = "none")]
870+
pub fn extend_from_within<R>(&mut self, src: R)
871+
where
872+
R: RangeBounds<usize>,
873+
{
874+
let src @ Range { start, end } = slice::range(src, ..self.len());
875+
876+
assert!(self.is_char_boundary(start));
877+
assert!(self.is_char_boundary(end));
878+
879+
self.vec.extend_from_within(src);
880+
}
881+
846882
/// Returns this `String`'s capacity, in bytes.
847883
///
848884
/// # Examples

0 commit comments

Comments
 (0)