Add doc example for CStr::to_string_lossy.

This commit is contained in:
Corey Farwell 2017-06-18 14:46:19 -07:00
parent 5d71e8cd7e
commit 82ba871c70

View file

@ -893,6 +893,31 @@ pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
///
/// [`Cow`]: ../borrow/enum.Cow.html
/// [`str`]: ../primitive.str.html
///
/// # Examples
///
/// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
///
/// ```
/// use std::borrow::Cow;
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
/// assert_eq!(c_str.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 c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
/// assert_eq!(
/// c_str.to_string_lossy(),
/// Cow::Owned(String::from("Hello <20>World")) as Cow<str>
/// );
/// ```
#[stable(feature = "cstr_to_str", since = "1.4.0")]
pub fn to_string_lossy(&self) -> Cow<str> {
String::from_utf8_lossy(self.to_bytes())