diff options
| author | kennytm <kennytm@gmail.com> | 2018-03-28 17:55:09 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-28 17:55:09 +0200 |
| commit | 010fb40b4454a2dc278cbb8a636590d081f10295 (patch) | |
| tree | 264e28d5735f108f5c77186d6675ddd470a0d2a1 /src/liballoc/string.rs | |
| parent | 0214304b5aa74fd6beaefb2474d6ba90ea423a43 (diff) | |
| parent | 04f6692aaf78809c041ba6145bde2dcbeec9725e (diff) | |
| download | rust-010fb40b4454a2dc278cbb8a636590d081f10295.tar.gz rust-010fb40b4454a2dc278cbb8a636590d081f10295.zip | |
Rollup merge of #49400 - Diggsey:shrink-to, r=joshtriplett
Implement `shrink_to` method on collections Fixes #49385
Diffstat (limited to 'src/liballoc/string.rs')
| -rw-r--r-- | src/liballoc/string.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 599d66e8391..23c12bef3aa 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 |
