about summary refs log tree commit diff
path: root/src/libcollections
diff options
context:
space:
mode:
authorClar Charr <clar@charr.xyz>2016-11-28 13:54:55 -0500
committerClar Charr <clar@charr.xyz>2016-11-30 23:24:57 -0500
commitcbf734f9abe06d240db086f55a0bd4387b77eb94 (patch)
tree1017c3ff77b3c65a1c2e797c2d960cca530919d2 /src/libcollections
parentdc81742b1869001e1926f130623eb5b8cf16e7a9 (diff)
Add String::split_off.
Diffstat (limited to 'src/libcollections')
-rw-r--r--src/libcollections/string.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs
index 348eb6fb5ff..ddde9d06d81 100644
--- a/src/libcollections/string.rs
+++ b/src/libcollections/string.rs
@@ -1260,6 +1260,39 @@ impl String {
         self.len() == 0
     }
 
+    /// Divide one string into two at an index.
+    ///
+    /// The argument, `mid`, should be a byte offset from the start of the string. It must also
+    /// be on the boundary of a UTF-8 code point.
+    ///
+    /// The two strings returned go from the start of the string to `mid`, and from `mid` to the end
+    /// of the string.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `mid` is not on a `UTF-8` code point boundary, or if it is beyond the last
+    /// code point of the string.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # #![feature(string_split_off)]
+    /// # fn main() {
+    /// let mut hello = String::from("Hello, World!");
+    /// let world = hello.split_off(7);
+    /// assert_eq!(hello, "Hello, ");
+    /// assert_eq!(world, "World!");
+    /// # }
+    /// ```
+    #[inline]
+    #[unstable(feature = "string_split_off", issue = "38080")]
+    pub fn split_off(&mut self, mid: usize) -> String {
+        assert!(self.is_char_boundary(mid));
+        assert!(mid <= self.len());
+        let other = self.vec.split_off(mid);
+        unsafe { String::from_utf8_unchecked(other) }
+    }
+
     /// Truncates this `String`, removing all contents.
     ///
     /// While this means the `String` will have a length of zero, it does not