diff options
| author | 许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com> | 2024-05-18 20:38:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-18 20:38:05 +0100 |
| commit | c367d9925947f77a4e102f7d132b064cc22cfaf9 (patch) | |
| tree | 8a646a22705118c2d95c4a4a9438c7428a7c4619 | |
| parent | ffc04dfcc6d6e0ecae8a3c44978e6db9fbc26718 (diff) | |
| parent | 0beba9699c6a30dfbf1241e3ca264d7a2b23d5f7 (diff) | |
| download | rust-c367d9925947f77a4e102f7d132b064cc22cfaf9.tar.gz rust-c367d9925947f77a4e102f7d132b064cc22cfaf9.zip | |
Rollup merge of #125251 - jonhoo:patch-1, r=Nilstrieb
Clarify how String::leak and into_boxed_str differ
| -rw-r--r-- | library/alloc/src/string.rs | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 2a859ad55ee..335a1a8ffb3 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -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 @@ -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 /// |
