@@ -67,7 +67,7 @@ use core::str::Utf8Chunks;
67
67
use crate :: borrow:: { Cow , ToOwned } ;
68
68
use crate :: boxed:: Box ;
69
69
use crate :: collections:: TryReserveError ;
70
- use crate :: str:: { self , Chars , Utf8Error } ;
70
+ use crate :: str:: { self , from_utf8_unchecked_mut , Chars , Utf8Error } ;
71
71
#[ cfg( not( no_global_oom_handling) ) ]
72
72
use crate :: str:: { from_boxed_utf8_unchecked, FromStr } ;
73
73
use crate :: vec:: Vec ;
@@ -2691,6 +2691,35 @@ impl From<String> for Box<str> {
2691
2691
fn from ( s : String ) -> Box < str > {
2692
2692
s. into_boxed_str ( )
2693
2693
}
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
+ }
2694
2723
}
2695
2724
2696
2725
#[ cfg( not( no_global_oom_handling) ) ]
0 commit comments