From 22fc094000678b15278360f112c44059c8af5f4b Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Sat, 18 May 2024 17:46:31 +0200 Subject: [PATCH] Update string.rs --- library/alloc/src/string.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 39395426703b..7d6606550c88 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1941,6 +1941,7 @@ impl String { /// Converts this `String` into a [Box]<[str]>. /// /// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`]. + /// Note that the [`shrink_to_fit`] call may reallocate and copy the bytes of the string. /// /// [`shrink_to_fit`]: String::shrink_to_fit /// [str]: prim@str "str" @@ -1968,13 +1969,10 @@ impl String { /// this function is ideally used for data that lives for the remainder of the program's life, /// as dropping the returned reference will cause a memory leak. /// - /// It does not reallocate or shrink the `String`, - /// so the leaked allocation may include unused capacity that is not part - /// of the returned slice. The advantage of this is that it avoids the potential - /// reallocation that [`shrink_to_fit`] may perform. - /// - /// If you want to discard excess capacity, call [`into_boxed_str`], - /// and then [`Box::leak`] instead. + /// It does not reallocate or shrink the `String`, so the leaked allocation may include unused + /// capacity that is not part of the returned slice. If you want to discard excess capacity, + /// call [`into_boxed_str`], and then [`Box::leak`] instead. However, keep in mind that + /// trimming the capacity may result in a reallocation and copy. /// /// [`into_boxed_str`]: Self::into_boxed_str ///