Skip to content

Commit f81cd87

Browse files
committed
1 parent 57781b2 commit f81cd87

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

library/alloc/src/string.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use core::str::Utf8Chunks;
6767
use crate::borrow::{Cow, ToOwned};
6868
use crate::boxed::Box;
6969
use crate::collections::TryReserveError;
70-
use crate::str::{self, Chars, Utf8Error};
70+
use crate::str::{self, from_utf8_unchecked_mut, Chars, Utf8Error};
7171
#[cfg(not(no_global_oom_handling))]
7272
use crate::str::{from_boxed_utf8_unchecked, FromStr};
7373
use crate::vec::Vec;
@@ -2691,6 +2691,35 @@ impl From<String> for Box<str> {
26912691
fn from(s: String) -> Box<str> {
26922692
s.into_boxed_str()
26932693
}
2694+
2695+
/// Consumes and leaks the `String`, returning a mutable reference to the contents,
2696+
/// `&'a mut str`.
2697+
///
2698+
/// This is mainly useful for data that lives for the remainder of
2699+
/// the program's life. Dropping the returned reference will cause a memory
2700+
/// leak.
2701+
///
2702+
/// It does not reallocate or shrink the `String`,
2703+
/// so the leaked allocation may include unused capacity that is not part
2704+
/// of the returned slice.
2705+
///
2706+
/// # Examples
2707+
///
2708+
/// Simple usage:
2709+
///
2710+
/// ```
2711+
/// #![feature(string_leak)]
2712+
///
2713+
/// let x = String::from("bucket");
2714+
/// let static_ref: &'static mut str = x.leak();
2715+
/// assert_eq!(static_ref, "bucket");
2716+
/// ```
2717+
#[unstable(feature = "string_leak", issue = "102929")]
2718+
#[inline]
2719+
pub fn leak(self) -> &'static mut str {
2720+
let slice = self.vec.leak();
2721+
unsafe { from_utf8_unchecked_mut(slice) }
2722+
}
26942723
}
26952724

26962725
#[cfg(not(no_global_oom_handling))]

0 commit comments

Comments
 (0)