]> git.lizzy.rs Git - rust.git/commitdiff
Update docs for `CStr::from_ptr`.
authorMarkus Reiter <me@reitermark.us>
Wed, 12 Oct 2022 11:12:27 +0000 (13:12 +0200)
committerMarkus Reiter <me@reitermark.us>
Wed, 12 Oct 2022 11:46:20 +0000 (13:46 +0200)
library/core/src/ffi/c_str.rs

index ae15be793414e26805c131a6c5bcab533046cb5d..9f7c960d88feb6463d69161eb1d2794b72197dbb 100644 (file)
@@ -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