Skip to content

Commit 5d71e8c

Browse files
committed
Add error scenario doc examples for CStr::from_bytes_with_nul.
1 parent 7f687f8 commit 5d71e8c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,24 @@ impl CStr {
708708
/// let cstr = CStr::from_bytes_with_nul(b"hello\0");
709709
/// assert!(cstr.is_ok());
710710
/// ```
711+
///
712+
/// Creating a `CStr` without a trailing nul byte is an error:
713+
///
714+
/// ```
715+
/// use std::ffi::CStr;
716+
///
717+
/// let c_str = CStr::from_bytes_with_nul(b"hello");
718+
/// assert!(c_str.is_err());
719+
/// ```
720+
///
721+
/// Creating a `CStr` with an interior nul byte is an error:
722+
///
723+
/// ```
724+
/// use std::ffi::CStr;
725+
///
726+
/// let c_str = CStr::from_bytes_with_nul(b"he\0llo\0");
727+
/// assert!(c_str.is_err());
728+
/// ```
711729
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
712730
pub fn from_bytes_with_nul(bytes: &[u8])
713731
-> Result<&CStr, FromBytesWithNulError> {

0 commit comments

Comments
 (0)