Skip to content

Commit e52d2f2

Browse files
committed
Add doc example for CStr::to_str.
1 parent 0962394 commit e52d2f2

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,15 @@ impl CStr {
894894
/// > check whenever this method is called.
895895
///
896896
/// [`&str`]: ../primitive.str.html
897+
///
898+
/// # Examples
899+
///
900+
/// ```
901+
/// use std::ffi::CStr;
902+
///
903+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
904+
/// assert_eq!(c_str.to_str(), Ok("foo"));
905+
/// ```
897906
#[stable(feature = "cstr_to_str", since = "1.4.0")]
898907
pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
899908
// NB: When CStr is changed to perform the length check in .to_bytes()

0 commit comments

Comments
 (0)