@@ -321,7 +321,7 @@ mod spec_extend;
321
321
/// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec`
322
322
/// and then filling it back up to the same [`len`] should incur no calls to
323
323
/// the allocator. If you wish to free up unused memory, use
324
- /// [`shrink_to_fit`].
324
+ /// [`shrink_to_fit`] or [`shrink_to`] .
325
325
///
326
326
/// [`push`] and [`insert`] will never (re)allocate if the reported capacity is
327
327
/// sufficient. [`push`] and [`insert`] *will* (re)allocate if
@@ -360,6 +360,7 @@ mod spec_extend;
360
360
/// [`String`]: crate::string::String
361
361
/// [`&str`]: type@str
362
362
/// [`shrink_to_fit`]: Vec::shrink_to_fit
363
+ /// [`shrink_to`]: Vec::shrink_to
363
364
/// [`capacity`]: Vec::capacity
364
365
/// [`mem::size_of::<T>`]: core::mem::size_of
365
366
/// [`len`]: Vec::len
@@ -909,10 +910,7 @@ impl<T, A: Allocator> Vec<T, A> {
909
910
/// The capacity will remain at least as large as both the length
910
911
/// and the supplied value.
911
912
///
912
- /// # Panics
913
- ///
914
- /// Panics if the current capacity is smaller than the supplied
915
- /// minimum capacity.
913
+ /// If the current capacity is less than the lower limit, this is a no-op.
916
914
///
917
915
/// # Examples
918
916
///
@@ -929,7 +927,9 @@ impl<T, A: Allocator> Vec<T, A> {
929
927
#[ doc( alias = "realloc" ) ]
930
928
#[ unstable( feature = "shrink_to" , reason = "new API" , issue = "56431" ) ]
931
929
pub fn shrink_to ( & mut self , min_capacity : usize ) {
932
- self . buf . shrink_to_fit ( cmp:: max ( self . len , min_capacity) ) ;
930
+ if self . capacity ( ) > min_capacity {
931
+ self . buf . shrink_to_fit ( cmp:: max ( self . len , min_capacity) ) ;
932
+ }
933
933
}
934
934
935
935
/// Converts the vector into [`Box<[T]>`][owned slice].
0 commit comments