diff options
| author | George Bateman <george.bateman16@gmail.com> | 2024-05-01 19:28:45 +0100 |
|---|---|---|
| committer | George Bateman <george.bateman16@gmail.com> | 2024-05-01 19:59:00 +0100 |
| commit | e610a52a6259cae4c9f3ebdb6016e68b507878e4 (patch) | |
| tree | 8df513a756d9231eb3ef5aabba2ea13fa392fad6 /library/alloc | |
| parent | 20aa2d81e36036073a9acf418c7d413cb4b22fa6 (diff) | |
| download | rust-e610a52a6259cae4c9f3ebdb6016e68b507878e4.tar.gz rust-e610a52a6259cae4c9f3ebdb6016e68b507878e4.zip | |
Describe and use CStr literals in CStr and CString docs
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/ffi/c_str.rs | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 6a64eaf576b..f143e557871 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -41,6 +41,7 @@ use crate::sync::Arc; /// or anything that implements <code>[Into]<[Vec]<[u8]>></code> (for /// example, you can build a `CString` straight out of a [`String`] or /// a <code>&[str]</code>, since both implement that trait). +/// You can create a `CString` from a literal with `CString::from(c"Text")`. /// /// The [`CString::new`] method will actually check that the provided <code>&[[u8]]</code> /// does not have 0 bytes in the middle, and return an error if it @@ -1069,27 +1070,22 @@ impl CStr { /// /// # Examples /// - /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8: + /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8. The leading + /// `c` on the string literal denotes a `CStr`. /// /// ``` /// use std::borrow::Cow; - /// use std::ffi::CStr; /// - /// let cstr = CStr::from_bytes_with_nul(b"Hello World\0") - /// .expect("CStr::from_bytes_with_nul failed"); - /// assert_eq!(cstr.to_string_lossy(), Cow::Borrowed("Hello World")); + /// assert_eq!(c"Hello World".to_string_lossy(), Cow::Borrowed("Hello World")); /// ``` /// /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8: /// /// ``` /// use std::borrow::Cow; - /// use std::ffi::CStr; /// - /// let cstr = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0") - /// .expect("CStr::from_bytes_with_nul failed"); /// assert_eq!( - /// cstr.to_string_lossy(), + /// c"Hello \xF0\x90\x80World".to_string_lossy(), /// Cow::Owned(String::from("Hello �World")) as Cow<'_, str> /// ); /// ``` |
