Rollup merge of #99781 - workingjubilee:demo-string-from-cstr, r=thomcc

Use String::from_utf8_lossy in CStr demo

Fixes rust-lang/rust#99755.
This commit is contained in:
Yuki Okushi 2022-07-29 15:40:00 +09:00 committed by GitHub
commit 9b3f49f1bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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());