@@ -455,13 +455,13 @@ impl String {
455455 String { vec : Vec :: new ( ) }
456456 }
457457
458- /// Creates a new empty `String` with a particular capacity.
458+ /// Creates a new empty `String` with at least the specified capacity.
459459 ///
460460 /// `String`s have an internal buffer to hold their data. The capacity is
461461 /// the length of that buffer, and can be queried with the [`capacity`]
462462 /// method. This method creates an empty `String`, but one with an initial
463- /// buffer that can hold `capacity` bytes. This is useful when you may be
464- /// appending a bunch of data to the `String`, reducing the number of
463+ /// buffer that can hold at least `capacity` bytes. This is useful when you
464+ /// may be appending a bunch of data to the `String`, reducing the number of
465465 /// reallocations it needs to do.
466466 ///
467467 /// [`capacity`]: String::capacity
@@ -979,21 +979,16 @@ impl String {
979979 self . vec . capacity ( )
980980 }
981981
982- /// Ensures that this `String`'s capacity is at least `additional` bytes
983- /// larger than its length.
984- ///
985- /// The capacity may be increased by more than `additional` bytes if it
986- /// chooses, to prevent frequent reallocations.
987- ///
988- /// If you do not want this "at least" behavior, see the [`reserve_exact`]
989- /// method.
982+ /// Reserves capacity for at least `additional` bytes more than the
983+ /// current length. The allocator may reserve more space to speculatively
984+ /// avoid frequent allocations. After calling `reserve`,
985+ /// capacity will be greater than or equal to `self.len() + additional`.
986+ /// Does nothing if capacity is already sufficient.
990987 ///
991988 /// # Panics
992989 ///
993990 /// Panics if the new capacity overflows [`usize`].
994991 ///
995- /// [`reserve_exact`]: String::reserve_exact
996- ///
997992 /// # Examples
998993 ///
999994 /// Basic usage:
@@ -1013,15 +1008,16 @@ impl String {
10131008 /// s.push('a');
10141009 /// s.push('b');
10151010 ///
1016- /// // s now has a length of 2 and a capacity of 10
1011+ /// // s now has a length of 2 and a capacity of at least 10
1012+ /// let capacity = s.capacity();
10171013 /// assert_eq!(2, s.len());
1018- /// assert_eq!(10, s. capacity() );
1014+ /// assert!( capacity >= 10 );
10191015 ///
1020- /// // Since we already have an extra 8 capacity, calling this...
1016+ /// // Since we already have at least an extra 8 capacity, calling this...
10211017 /// s.reserve(8);
10221018 ///
10231019 /// // ... doesn't actually increase.
1024- /// assert_eq!(10 , s.capacity());
1020+ /// assert_eq!(capacity , s.capacity());
10251021 /// ```
10261022 #[ cfg( not( no_global_oom_handling) ) ]
10271023 #[ inline]
@@ -1030,17 +1026,18 @@ impl String {
10301026 self . vec . reserve ( additional)
10311027 }
10321028
1033- /// Ensures that this `String`'s capacity is `additional` bytes
1034- /// larger than its length.
1035- ///
1036- /// Consider using the [`reserve`] method unless you absolutely know
1037- /// better than the allocator.
1029+ /// Reserves the minimum capacity for at least `additional` bytes more than
1030+ /// the current length. Unlike [`reserve`], this will not
1031+ /// deliberately over-allocate to speculatively avoid frequent allocations.
1032+ /// After calling `reserve_exact`, capacity will be greater than or equal to
1033+ /// `self.len() + additional`. Does nothing if the capacity is already
1034+ /// sufficient.
10381035 ///
10391036 /// [`reserve`]: String::reserve
10401037 ///
10411038 /// # Panics
10421039 ///
1043- /// Panics if the new capacity overflows `usize`.
1040+ /// Panics if the new capacity overflows [ `usize`] .
10441041 ///
10451042 /// # Examples
10461043 ///
@@ -1061,15 +1058,16 @@ impl String {
10611058 /// s.push('a');
10621059 /// s.push('b');
10631060 ///
1064- /// // s now has a length of 2 and a capacity of 10
1061+ /// // s now has a length of 2 and a capacity of at least 10
1062+ /// let capacity = s.capacity();
10651063 /// assert_eq!(2, s.len());
1066- /// assert_eq!(10, s. capacity() );
1064+ /// assert!( capacity >= 10 );
10671065 ///
1068- /// // Since we already have an extra 8 capacity, calling this...
1066+ /// // Since we already have at least an extra 8 capacity, calling this...
10691067 /// s.reserve_exact(8);
10701068 ///
10711069 /// // ... doesn't actually increase.
1072- /// assert_eq!(10 , s.capacity());
1070+ /// assert_eq!(capacity , s.capacity());
10731071 /// ```
10741072 #[ cfg( not( no_global_oom_handling) ) ]
10751073 #[ inline]
@@ -1078,11 +1076,11 @@ impl String {
10781076 self . vec . reserve_exact ( additional)
10791077 }
10801078
1081- /// Tries to reserve capacity for at least `additional` more elements to be inserted
1082- /// in the given `String` . The collection may reserve more space to avoid
1083- /// frequent reallocations . After calling `reserve `, capacity will be
1084- /// greater than or equal to `self.len() + additional`. Does nothing if
1085- /// capacity is already sufficient.
1079+ /// Tries to reserve capacity for at least `additional` bytes more than the
1080+ /// current length . The allocator may reserve more space to speculatively
1081+ /// avoid frequent allocations . After calling `try_reserve `, capacity will be
1082+ /// greater than or equal to `self.len() + additional` if it returns
1083+ /// `Ok(())`. Does nothing if capacity is already sufficient.
10861084 ///
10871085 /// # Errors
10881086 ///
@@ -1112,9 +1110,11 @@ impl String {
11121110 self . vec . try_reserve ( additional)
11131111 }
11141112
1115- /// Tries to reserve the minimum capacity for exactly `additional` more elements to
1116- /// be inserted in the given `String`. After calling `try_reserve_exact`,
1117- /// capacity will be greater than or equal to `self.len() + additional`.
1113+ /// Tries to reserve the minimum capacity for at least `additional` bytes
1114+ /// more than the current length. Unlike [`try_reserve`], this will not
1115+ /// deliberately over-allocate to speculatively avoid frequent allocations.
1116+ /// After calling `try_reserve_exact`, capacity will be greater than or
1117+ /// equal to `self.len() + additional` if it returns `Ok(())`.
11181118 /// Does nothing if the capacity is already sufficient.
11191119 ///
11201120 /// Note that the allocator may give the collection more space than it
0 commit comments