Skip to content

Commit

Permalink
Rollup merge of #125251 - jonhoo:patch-1, r=Nilstrieb
Browse files Browse the repository at this point in the history
Clarify how String::leak and into_boxed_str differ
  • Loading branch information
jieyouxu authored May 18, 2024
2 parents ffc04df + 0beba96 commit c367d99
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,8 +1940,10 @@ impl String {

/// Converts this `String` into a <code>[Box]<[str]></code>.
///
/// This will drop any excess capacity.
/// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].
/// Note that this call may reallocate and copy the bytes of the string.
///
/// [`shrink_to_fit`]: String::shrink_to_fit
/// [str]: prim@str "str"
///
/// # Examples
Expand All @@ -1967,10 +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. If you don't want that, call [`into_boxed_str`],
/// and then [`Box::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. 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
///
Expand Down

0 comments on commit c367d99

Please sign in to comment.