summary refs log tree commit diff
path: root/library/alloc/src/string.rs
diff options
context:
space:
mode:
authorWaffle <waffle.lapkin@gmail.com>2021-05-29 10:36:30 +0300
committerWaffle <waffle.lapkin@gmail.com>2021-05-29 10:36:30 +0300
commit23f9b92c5e3fd6dcff19fb185b64990f0336a8bf (patch)
treec5e38deb6914dafc45d1808a4c15fe9b49a7b960 /library/alloc/src/string.rs
parent4664725ae03ef9becae413a1e56b5010f88fdc46 (diff)
downloadrust-23f9b92c5e3fd6dcff19fb185b64990f0336a8bf.tar.gz
rust-23f9b92c5e3fd6dcff19fb185b64990f0336a8bf.zip
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.
Diffstat (limited to 'library/alloc/src/string.rs')
-rw-r--r--library/alloc/src/string.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index ec09595e357..dbe5bc1da46 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -843,6 +843,42 @@ impl String {
         self.vec.extend_from_slice(string.as_bytes())
     }
 
+    /// Copies elements from `src` range to the end of the string.
+    ///
+    /// ## Panics
+    ///
+    /// Panics if the starting point or end point do not lie on a [`char`]
+    /// boundary, or if they're out of bounds.
+    ///
+    /// ## Examples
+    ///
+    /// ```
+    /// #![feature(string_extend_from_within)]
+    /// let mut string = String::from("abcde");
+    ///
+    /// string.extend_from_within(2..);
+    /// assert_eq!(string, "abcdecde");
+    ///
+    /// string.extend_from_within(..2);
+    /// assert_eq!(string, "abcdecdeab");
+    ///
+    /// string.extend_from_within(4..8);
+    /// assert_eq!(string, "abcdecdeabecde");
+    /// ```
+    #[cfg(not(no_global_oom_handling))]
+    #[unstable(feature = "string_extend_from_within", issue = "none")]
+    pub fn extend_from_within<R>(&mut self, src: R)
+    where
+        R: RangeBounds<usize>,
+    {
+        let src @ Range { start, end } = slice::range(src, ..self.len());
+
+        assert!(self.is_char_boundary(start));
+        assert!(self.is_char_boundary(end));
+
+        self.vec.extend_from_within(src);
+    }
+
     /// Returns this `String`'s capacity, in bytes.
     ///
     /// # Examples