Skip to content

Commit 9b3f49f

Browse files
authored
Rollup merge of #99781 - workingjubilee:demo-string-from-cstr, r=thomcc
Use String::from_utf8_lossy in CStr demo Fixes #99755.
2 parents 55296c4 + d48a869 commit 9b3f49f

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

library/core/src/ffi/c_str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ use crate::str;
6565
/// extern "C" { fn my_string() -> *const c_char; }
6666
///
6767
/// fn my_string_safe() -> String {
68-
/// unsafe {
69-
/// CStr::from_ptr(my_string()).to_string_lossy().into_owned()
70-
/// }
68+
/// let cstr = unsafe { CStr::from_ptr(my_string()) };
69+
/// // Get copy-on-write Cow<'_, str>, then guarantee a freshly-owned String allocation
70+
/// String::from_utf8_lossy(cstr.to_bytes()).to_string()
7171
/// }
7272
///
7373
/// println!("string: {}", my_string_safe());

0 commit comments

Comments
 (0)