about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee Young <workingjubilee@gmail.com>2022-07-26 14:10:26 -0700
committerJubilee Young <workingjubilee@gmail.com>2022-07-26 14:45:28 -0700
commitd48a869b9db1196214e19d1330cb290fc8543683 (patch)
treead063abe43c51b373cc229d3512415c82fc33ae6
parent79e05430600dca42571ba38703af2969bab48760 (diff)
downloadrust-d48a869b9db1196214e19d1330cb290fc8543683.tar.gz
rust-d48a869b9db1196214e19d1330cb290fc8543683.zip
Force the Cow into a String
-rw-r--r--library/core/src/ffi/c_str.rs6
1 files changed, 3 insertions, 3 deletions
diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs
index 1deb41593d9..59066a33c96 100644
--- a/library/core/src/ffi/c_str.rs
+++ b/library/core/src/ffi/c_str.rs
@@ -65,9 +65,9 @@ use crate::str;
 /// extern "C" { fn my_string() -> *const c_char; }
 ///
 /// fn my_string_safe() -> String {
-///     unsafe {
-///         String::from_utf8_lossy(CStr::from_ptr(my_string()))
-///     }
+///     let cstr = unsafe { CStr::from_ptr(my_string()) };
+///     // Get copy-on-write Cow<'_, str>, then guarantee a freshly-owned String allocation
+///     String::from_utf8_lossy(cstr.to_bytes()).to_string()
 /// }
 ///
 /// println!("string: {}", my_string_safe());