about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJake Goulding <jake.goulding@gmail.com>2025-04-25 11:27:01 -0400
committerJake Goulding <jake.goulding@gmail.com>2025-04-26 12:38:23 -0400
commit9112c86f4226a6922f07bb44b49cedb75d1ee2f9 (patch)
treee1363b32e8bd18a308a8edc23eb6c8edf7e3dde7
parent5c54aa781f97ae95ef01a3120650907b87d385d3 (diff)
downloadrust-9112c86f4226a6922f07bb44b49cedb75d1ee2f9.tar.gz
rust-9112c86f4226a6922f07bb44b49cedb75d1ee2f9.zip
Update example to use `CStr::to_string_lossy`
-rw-r--r--library/core/src/ffi/c_str.rs5
1 files changed, 3 insertions, 2 deletions
diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs
index 85e87445a1b..ac07c645c01 100644
--- a/library/core/src/ffi/c_str.rs
+++ b/library/core/src/ffi/c_str.rs
@@ -79,8 +79,9 @@ use crate::{fmt, ops, slice, str};
 ///
 /// fn my_string_safe() -> String {
 ///     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()
+///     // Get a copy-on-write Cow<'_, str>, then extract the
+///     // allocated String (or allocate a fresh one if needed).
+///     cstr.to_string_lossy().into_owned()
 /// }
 ///
 /// println!("string: {}", my_string_safe());