about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarkus Reiter <me@reitermark.us>2022-10-12 13:12:27 +0200
committerMarkus Reiter <me@reitermark.us>2022-10-12 13:46:20 +0200
commit36dbb07dafe59c4d1491c2fbff6971d653f2ff77 (patch)
tree06b1cd1a34f571bf4a34c99f9142cf405633d1e8
parent328f81713cc80861fb79c629e4887de1a8b7bf05 (diff)
downloadrust-36dbb07dafe59c4d1491c2fbff6971d653f2ff77.tar.gz
rust-36dbb07dafe59c4d1491c2fbff6971d653f2ff77.zip
Update docs for `CStr::from_ptr`.
-rw-r--r--library/core/src/ffi/c_str.rs17
1 files changed, 13 insertions, 4 deletions
diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs
index ae15be79341..9f7c960d88f 100644
--- a/library/core/src/ffi/c_str.rs
+++ b/library/core/src/ffi/c_str.rs
@@ -221,9 +221,7 @@ impl CStr {
     /// # Examples
     ///
     /// ```ignore (extern-declaration)
-    /// # fn main() {
-    /// use std::ffi::CStr;
-    /// use std::os::raw::c_char;
+    /// use std::ffi::{c_char, CStr};
     ///
     /// extern "C" {
     ///     fn my_string() -> *const c_char;
@@ -233,7 +231,18 @@ impl CStr {
     ///     let slice = CStr::from_ptr(my_string());
     ///     println!("string returned: {}", slice.to_str().unwrap());
     /// }
-    /// # }
+    /// ```
+    ///
+    /// ```
+    /// #![feature(const_cstr_methods)]
+    ///
+    /// use std::ffi::{c_char, CStr};
+    ///
+    /// const HELLO_PTR: *const c_char = {
+    ///     const BYTES: &[u8] = b"Hello, world!\0";
+    ///     BYTES.as_ptr().cast()
+    /// };
+    /// const HELLO: &CStr = unsafe { CStr::from_ptr(HELLO_PTR) };
     /// ```
     ///
     /// [valid]: core::ptr#safety