Update docs for CStr::from_ptr.

This commit is contained in:
Markus Reiter 2022-10-12 13:12:27 +02:00
parent 328f81713c
commit 36dbb07daf
No known key found for this signature in database
GPG Key ID: 245293B51702655B

View File

@ -221,9 +221,7 @@ impl CStr {
/// # Examples /// # Examples
/// ///
/// ```ignore (extern-declaration) /// ```ignore (extern-declaration)
/// # fn main() { /// use std::ffi::{c_char, CStr};
/// use std::ffi::CStr;
/// use std::os::raw::c_char;
/// ///
/// extern "C" { /// extern "C" {
/// fn my_string() -> *const c_char; /// fn my_string() -> *const c_char;
@ -233,7 +231,18 @@ impl CStr {
/// let slice = CStr::from_ptr(my_string()); /// let slice = CStr::from_ptr(my_string());
/// println!("string returned: {}", slice.to_str().unwrap()); /// 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 /// [valid]: core::ptr#safety