]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/ffi/c_str.rs
Rollup merge of #99904 - GuillaumeGomez:cleanup-html-whitespace, r=notriddle
[rust.git] / library / core / src / ffi / c_str.rs
index ee9baf811e29c099c70d18323978f655a98e65a6..59066a33c965eecce361b9869e2b593d3b7a775a 100644 (file)
@@ -65,9 +65,9 @@
 /// extern "C" { fn my_string() -> *const c_char; }
 ///
 /// fn my_string_safe() -> String {
-///     unsafe {
-///         CStr::from_ptr(my_string()).to_string_lossy().into_owned()
-///     }
+///     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());