Skip to content

Commit 36dbb07

Browse files
committed
Update docs for CStr::from_ptr.
1 parent 328f817 commit 36dbb07

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

library/core/src/ffi/c_str.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ impl CStr {
221221
/// # Examples
222222
///
223223
/// ```ignore (extern-declaration)
224-
/// # fn main() {
225-
/// use std::ffi::CStr;
226-
/// use std::os::raw::c_char;
224+
/// use std::ffi::{c_char, CStr};
227225
///
228226
/// extern "C" {
229227
/// fn my_string() -> *const c_char;
@@ -233,7 +231,18 @@ impl CStr {
233231
/// let slice = CStr::from_ptr(my_string());
234232
/// println!("string returned: {}", slice.to_str().unwrap());
235233
/// }
236-
/// # }
234+
/// ```
235+
///
236+
/// ```
237+
/// #![feature(const_cstr_methods)]
238+
///
239+
/// use std::ffi::{c_char, CStr};
240+
///
241+
/// const HELLO_PTR: *const c_char = {
242+
/// const BYTES: &[u8] = b"Hello, world!\0";
243+
/// BYTES.as_ptr().cast()
244+
/// };
245+
/// const HELLO: &CStr = unsafe { CStr::from_ptr(HELLO_PTR) };
237246
/// ```
238247
///
239248
/// [valid]: core::ptr#safety

0 commit comments

Comments
 (0)