@@ -455,13 +455,13 @@ impl String {
455
455
String { vec : Vec :: new ( ) }
456
456
}
457
457
458
- /// Creates a new empty `String` with a particular capacity.
458
+ /// Creates a new empty `String` with at least the specified capacity.
459
459
///
460
460
/// `String`s have an internal buffer to hold their data. The capacity is
461
461
/// the length of that buffer, and can be queried with the [`capacity`]
462
462
/// 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
465
465
/// reallocations it needs to do.
466
466
///
467
467
/// [`capacity`]: String::capacity
@@ -979,21 +979,16 @@ impl String {
979
979
self . vec . capacity ( )
980
980
}
981
981
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.
990
987
///
991
988
/// # Panics
992
989
///
993
990
/// Panics if the new capacity overflows [`usize`].
994
991
///
995
- /// [`reserve_exact`]: String::reserve_exact
996
- ///
997
992
/// # Examples
998
993
///
999
994
/// Basic usage:
@@ -1013,15 +1008,16 @@ impl String {
1013
1008
/// s.push('a');
1014
1009
/// s.push('b');
1015
1010
///
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();
1017
1013
/// assert_eq!(2, s.len());
1018
- /// assert_eq!(10, s. capacity() );
1014
+ /// assert!( capacity >= 10 );
1019
1015
///
1020
- /// // Since we already have an extra 8 capacity, calling this...
1016
+ /// // Since we already have at least an extra 8 capacity, calling this...
1021
1017
/// s.reserve(8);
1022
1018
///
1023
1019
/// // ... doesn't actually increase.
1024
- /// assert_eq!(10 , s.capacity());
1020
+ /// assert_eq!(capacity , s.capacity());
1025
1021
/// ```
1026
1022
#[ cfg( not( no_global_oom_handling) ) ]
1027
1023
#[ inline]
@@ -1030,17 +1026,18 @@ impl String {
1030
1026
self . vec . reserve ( additional)
1031
1027
}
1032
1028
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.
1038
1035
///
1039
1036
/// [`reserve`]: String::reserve
1040
1037
///
1041
1038
/// # Panics
1042
1039
///
1043
- /// Panics if the new capacity overflows `usize`.
1040
+ /// Panics if the new capacity overflows [ `usize`] .
1044
1041
///
1045
1042
/// # Examples
1046
1043
///
@@ -1061,15 +1058,16 @@ impl String {
1061
1058
/// s.push('a');
1062
1059
/// s.push('b');
1063
1060
///
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();
1065
1063
/// assert_eq!(2, s.len());
1066
- /// assert_eq!(10, s. capacity() );
1064
+ /// assert!( capacity >= 10 );
1067
1065
///
1068
- /// // Since we already have an extra 8 capacity, calling this...
1066
+ /// // Since we already have at least an extra 8 capacity, calling this...
1069
1067
/// s.reserve_exact(8);
1070
1068
///
1071
1069
/// // ... doesn't actually increase.
1072
- /// assert_eq!(10 , s.capacity());
1070
+ /// assert_eq!(capacity , s.capacity());
1073
1071
/// ```
1074
1072
#[ cfg( not( no_global_oom_handling) ) ]
1075
1073
#[ inline]
@@ -1078,11 +1076,11 @@ impl String {
1078
1076
self . vec . reserve_exact ( additional)
1079
1077
}
1080
1078
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.
1086
1084
///
1087
1085
/// # Errors
1088
1086
///
@@ -1112,9 +1110,11 @@ impl String {
1112
1110
self . vec . try_reserve ( additional)
1113
1111
}
1114
1112
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(())`.
1118
1118
/// Does nothing if the capacity is already sufficient.
1119
1119
///
1120
1120
/// Note that the allocator may give the collection more space than it
0 commit comments