Skip to content

Commit d48a869

Browse files
Force the Cow into a String
1 parent 79e0543 commit d48a869

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-
/// String::from_utf8_lossy(CStr::from_ptr(my_string()))
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)