diff options
| author | Finn Bear <finnbearlabs@gmail.com> | 2022-10-19 19:15:58 -0700 |
|---|---|---|
| committer | Finn Bear <finnbearlabs@gmail.com> | 2022-10-19 19:15:58 -0700 |
| commit | 4f44d6253d02484400afa315a1f0306f4cf35d16 (patch) | |
| tree | 4c59212de63f58cbd913c914f90e77d2853a80d1 /library/alloc | |
| parent | f81cd87eeae2b02ebfcf309764cd9e648040b988 (diff) | |
| download | rust-4f44d6253d02484400afa315a1f0306f4cf35d16.tar.gz rust-4f44d6253d02484400afa315a1f0306f4cf35d16.zip | |
Put fn in the right place.
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/string.rs | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 9bda7f5dc37..209abfac6bb 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1849,6 +1849,35 @@ impl String { let slice = self.vec.into_boxed_slice(); unsafe { from_boxed_utf8_unchecked(slice) } } + + /// 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. + /// + /// It does not reallocate or shrink the `String`, + /// so the leaked allocation may include unused capacity that is not part + /// of the returned slice. + /// + /// # 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")] + #[inline] + pub fn leak(self) -> &'static mut str { + let slice = self.vec.leak(); + unsafe { from_utf8_unchecked_mut(slice) } + } } impl FromUtf8Error { @@ -2691,35 +2720,6 @@ impl From<String> for Box<str> { fn from(s: String) -> Box<str> { s.into_boxed_str() } - - /// 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. - /// - /// It does not reallocate or shrink the `String`, - /// so the leaked allocation may include unused capacity that is not part - /// of the returned slice. - /// - /// # 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")] - #[inline] - pub fn leak(self) -> &'static mut str { - let slice = self.vec.leak(); - unsafe { from_utf8_unchecked_mut(slice) } - } } #[cfg(not(no_global_oom_handling))] |
