Skip to content

Commit e5269fc

Browse files
authored
Make it clearer what String::leak is useful for
1 parent 685a80f commit e5269fc

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

library/alloc/src/string.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,8 +1940,9 @@ impl String {
19401940

19411941
/// Converts this `String` into a <code>[Box]<[str]></code>.
19421942
///
1943-
/// This will drop any excess capacity.
1943+
/// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].
19441944
///
1945+
/// [`shrink_to_fit`]: String::shrink_to_fit
19451946
/// [str]: prim@str "str"
19461947
///
19471948
/// # Examples
@@ -1969,8 +1970,11 @@ impl String {
19691970
///
19701971
/// It does not reallocate or shrink the `String`,
19711972
/// so the leaked allocation may include unused capacity that is not part
1972-
/// of the returned slice. If you don't want that, call [`into_boxed_str`],
1973-
/// and then [`Box::leak`].
1973+
/// of the returned slice. The advantage of this is that it avoids the potential
1974+
/// reallocation that [`shrink_to_fit`] may perform.
1975+
///
1976+
/// If you want to discard excess capacity, call [`into_boxed_str`],
1977+
/// and then [`Box::leak`] instead.
19741978
///
19751979
/// [`into_boxed_str`]: Self::into_boxed_str
19761980
///

0 commit comments

Comments
 (0)