@@ -321,7 +321,7 @@ mod spec_extend;
321321/// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec`
322322/// and then filling it back up to the same [`len`] should incur no calls to
323323/// the allocator. If you wish to free up unused memory, use
324- /// [`shrink_to_fit`].
324+ /// [`shrink_to_fit`] or [`shrink_to`] .
325325///
326326/// [`push`] and [`insert`] will never (re)allocate if the reported capacity is
327327/// sufficient. [`push`] and [`insert`] *will* (re)allocate if
@@ -360,6 +360,7 @@ mod spec_extend;
360360/// [`String`]: crate::string::String
361361/// [`&str`]: type@str
362362/// [`shrink_to_fit`]: Vec::shrink_to_fit
363+ /// [`shrink_to`]: Vec::shrink_to
363364/// [`capacity`]: Vec::capacity
364365/// [`mem::size_of::<T>`]: core::mem::size_of
365366/// [`len`]: Vec::len
@@ -909,10 +910,7 @@ impl<T, A: Allocator> Vec<T, A> {
909910 /// The capacity will remain at least as large as both the length
910911 /// and the supplied value.
911912 ///
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.
916914 ///
917915 /// # Examples
918916 ///
@@ -929,7 +927,9 @@ impl<T, A: Allocator> Vec<T, A> {
929927 #[ doc( alias = "realloc" ) ]
930928 #[ unstable( feature = "shrink_to" , reason = "new API" , issue = "56431" ) ]
931929 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+ }
933933 }
934934
935935 /// Converts the vector into [`Box<[T]>`][owned slice].
0 commit comments