@@ -23,28 +23,32 @@ use crate::str;
2323///
2424/// This type represents a borrowed reference to a nul-terminated
2525/// array of bytes. It can be constructed safely from a <code>&[[u8]]</code>
26- /// slice, or unsafely from a raw `*const c_char`. It can then be
27- /// converted to a Rust <code>&[str]</code> by performing UTF-8 validation, or
28- /// into an owned `CString`.
26+ /// slice, or unsafely from a raw `*const c_char`. It can be expressed as a
27+ /// literal in the form `c"Hello world"`.
28+ ///
29+ /// The `CStr` can then be converted to a Rust <code>&[str]</code> by performing
30+ /// UTF-8 validation, or into an owned `CString`.
2931///
3032/// `&CStr` is to `CString` as <code>&[str]</code> is to `String`: the former
3133/// in each pair are borrowed references; the latter are owned
3234/// strings.
3335///
3436/// Note that this structure does **not** have a guaranteed layout (the `repr(transparent)`
35- /// notwithstanding) and is not recommended to be placed in the signatures of FFI functions.
36- /// Instead, safe wrappers of FFI functions may leverage the unsafe [`CStr::from_ptr `] constructor
37- /// to provide a safe interface to other consumers.
37+ /// notwithstanding) and should not be placed in the signatures of FFI functions.
38+ /// Instead, safe wrappers of FFI functions may leverage [`CStr::as_ptr `] and the unsafe
39+ /// [`CStr::from_ptr`] constructor to provide a safe interface to other consumers.
3840///
3941/// # Examples
4042///
4143/// Inspecting a foreign C string:
4244///
43- /// ```ignore (extern-declaration)
45+ /// ```
4446/// use std::ffi::CStr;
4547/// use std::os::raw::c_char;
4648///
49+ /// # /* Extern functions are awkward in doc comments - fake it instead
4750/// extern "C" { fn my_string() -> *const c_char; }
51+ /// # */ unsafe extern "C" fn my_string() -> *const c_char { c"hello".as_ptr() }
4852///
4953/// unsafe {
5054/// let slice = CStr::from_ptr(my_string());
@@ -54,12 +58,14 @@ use crate::str;
5458///
5559/// Passing a Rust-originating C string:
5660///
57- /// ```ignore (extern-declaration)
61+ /// ```
5862/// use std::ffi::{CString, CStr};
5963/// use std::os::raw::c_char;
6064///
6165/// fn work(data: &CStr) {
66+ /// # /* Extern functions are awkward in doc comments - fake it instead
6267/// extern "C" { fn work_with(data: *const c_char); }
68+ /// # */ unsafe extern "C" fn work_with(s: *const c_char) {}
6369///
6470/// unsafe { work_with(data.as_ptr()) }
6571/// }
@@ -70,11 +76,13 @@ use crate::str;
7076///
7177/// Converting a foreign C string into a Rust `String`:
7278///
73- /// ```ignore (extern-declaration)
79+ /// ```
7480/// use std::ffi::CStr;
7581/// use std::os::raw::c_char;
7682///
83+ /// # /* Extern functions are awkward in doc comments - fake it instead
7784/// extern "C" { fn my_string() -> *const c_char; }
85+ /// # */ unsafe extern "C" fn my_string() -> *const c_char { c"hello".as_ptr() }
7886///
7987/// fn my_string_safe() -> String {
8088/// let cstr = unsafe { CStr::from_ptr(my_string()) };
@@ -241,16 +249,16 @@ impl CStr {
241249 ///
242250 /// # Examples
243251 ///
244- /// ```ignore (extern-declaration)
252+ /// ```
245253 /// use std::ffi::{c_char, CStr};
246254 ///
247- /// extern "C" {
248- /// fn my_string() -> *const c_char;
255+ /// fn my_string() -> *const c_char {
256+ /// c"hello".as_ptr()
249257 /// }
250258 ///
251259 /// unsafe {
252260 /// let slice = CStr::from_ptr(my_string());
253- /// println!("string returned: {}", slice.to_str().unwrap());
261+ /// assert_eq!( slice.to_str().unwrap(), "hello" );
254262 /// }
255263 /// ```
256264 ///
@@ -264,6 +272,8 @@ impl CStr {
264272 /// BYTES.as_ptr().cast()
265273 /// };
266274 /// const HELLO: &CStr = unsafe { CStr::from_ptr(HELLO_PTR) };
275+ ///
276+ /// assert_eq!(c"Hello, world!", HELLO);
267277 /// ```
268278 ///
269279 /// [valid]: core::ptr#safety
@@ -549,6 +559,7 @@ impl CStr {
549559 ///
550560 /// let empty_cstr = CStr::from_bytes_with_nul(b"\0")?;
551561 /// assert!(empty_cstr.is_empty());
562+ /// assert!(c"".is_empty());
552563 /// # Ok(())
553564 /// # }
554565 /// ```
0 commit comments