Add error scenario doc examples for CStr::from_bytes_with_nul.

This commit is contained in:
Corey Farwell 2017-06-18 14:28:10 -07:00
parent 7f687f8602
commit 5d71e8cd7e

View File

@ -708,6 +708,24 @@ pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
/// let cstr = CStr::from_bytes_with_nul(b"hello\0"); /// let cstr = CStr::from_bytes_with_nul(b"hello\0");
/// assert!(cstr.is_ok()); /// assert!(cstr.is_ok());
/// ``` /// ```
///
/// Creating a `CStr` without a trailing nul byte is an error:
///
/// ```
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"hello");
/// assert!(c_str.is_err());
/// ```
///
/// Creating a `CStr` with an interior nul byte is an error:
///
/// ```
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"he\0llo\0");
/// assert!(c_str.is_err());
/// ```
#[stable(feature = "cstr_from_bytes", since = "1.10.0")] #[stable(feature = "cstr_from_bytes", since = "1.10.0")]
pub fn from_bytes_with_nul(bytes: &[u8]) pub fn from_bytes_with_nul(bytes: &[u8])
-> Result<&CStr, FromBytesWithNulError> { -> Result<&CStr, FromBytesWithNulError> {