about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-07-29 15:40:00 +0900
committerGitHub <noreply@github.com>2022-07-29 15:40:00 +0900
commit9b3f49f1bd892dad768101b147a083e25d378619 (patch)
treeec1f8ede74dca549330c94d610fa93f7aefec6f5
parent55296c47f49fcb38d850ad16fb8d05efc3bdad1b (diff)
parentd48a869b9db1196214e19d1330cb290fc8543683 (diff)
downloadrust-9b3f49f1bd892dad768101b147a083e25d378619.tar.gz
rust-9b3f49f1bd892dad768101b147a083e25d378619.zip
Rollup merge of #99781 - workingjubilee:demo-string-from-cstr, r=thomcc
Use String::from_utf8_lossy in CStr demo

Fixes rust-lang/rust#99755.
-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 ee9baf811e2..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 {
-///         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());