Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize String::leak #109814

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1853,26 +1853,27 @@ impl String {
/// Consumes and leaks the `String`, returning a mutable reference to the contents,
/// `&'a mut str`.
///
/// This is mainly useful for data that lives for the remainder of
/// the program's life. Dropping the returned reference will cause a memory
/// leak.
/// The caller has free choice over the returned lifetime, including `'static`. Indeed,
/// 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.
/// of the returned slice. If you don't want that, call [`into_boxed_str`],
/// and then [`Box::leak`].
///
/// [`into_boxed_str`]: Self::into_boxed_str
///
/// # Examples
///
/// Simple usage:
///
/// ```
/// #![feature(string_leak)]
///
/// let x = String::from("bucket");
/// let static_ref: &'static mut str = x.leak();
/// assert_eq!(static_ref, "bucket");
/// ```
#[unstable(feature = "string_leak", issue = "102929")]
#[stable(feature = "string_leak", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub fn leak<'a>(self) -> &'a mut str {
let slice = self.vec.leak();
Expand Down