about summary refs log tree commit diff
path: root/src/liballoc/string.rs
diff options
context:
space:
mode:
authorDiggory Blake <diggsey@googlemail.com>2018-03-26 23:24:31 +0100
committerDiggory Blake <diggsey@googlemail.com>2018-03-27 01:39:11 +0100
commit04f6692aaf78809c041ba6145bde2dcbeec9725e (patch)
treeb02d9698234c023d503e8c7e5303eca5dc5caa1a /src/liballoc/string.rs
parentf5631d9ac7745dd6eaea2bc6c236d5f8e54e9a18 (diff)
downloadrust-04f6692aaf78809c041ba6145bde2dcbeec9725e.tar.gz
rust-04f6692aaf78809c041ba6145bde2dcbeec9725e.zip
Implement `shrink_to` method on collections
Diffstat (limited to 'src/liballoc/string.rs')
-rw-r--r--src/liballoc/string.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs
index e253122ffd6..2bb60a50679 100644
--- a/src/liballoc/string.rs
+++ b/src/liballoc/string.rs
@@ -1015,6 +1015,34 @@ impl String {
         self.vec.shrink_to_fit()
     }
 
+    /// Shrinks the capacity of this `String` with a lower bound.
+    ///
+    /// The capacity will remain at least as large as both the length
+    /// and the supplied value.
+    ///
+    /// Panics if the current capacity is smaller than the supplied
+    /// minimum capacity.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(shrink_to)]
+    /// let mut s = String::from("foo");
+    ///
+    /// s.reserve(100);
+    /// assert!(s.capacity() >= 100);
+    ///
+    /// s.shrink_to(10);
+    /// assert!(s.capacity() >= 10);
+    /// s.shrink_to(0);
+    /// assert!(s.capacity() >= 3);
+    /// ```
+    #[inline]
+    #[unstable(feature = "shrink_to", reason = "new API", issue="0")]
+    pub fn shrink_to(&mut self, min_capacity: usize) {
+        self.vec.shrink_to(min_capacity)
+    }
+
     /// Appends the given [`char`] to the end of this `String`.
     ///
     /// [`char`]: ../../std/primitive.char.html