summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-06-18 14:46:19 -0700
committerCorey Farwell <coreyf@rwell.org>2017-06-20 13:49:27 -0400
commit82ba871c7006b62e51b9036e6f20ae4e5015bd12 (patch)
tree91259039ea3a6c5336b8cddcd2f1618da34f938f /src/libstd
parent5d71e8cd7e25423e15400eaeb1bf6e4bf545c8e0 (diff)
downloadrust-82ba871c7006b62e51b9036e6f20ae4e5015bd12.tar.gz
rust-82ba871c7006b62e51b9036e6f20ae4e5015bd12.zip
Add doc example for `CStr::to_string_lossy`.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/ffi/c_str.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs
index ea0748d31e9..9b500a3d626 100644
--- a/src/libstd/ffi/c_str.rs
+++ b/src/libstd/ffi/c_str.rs
@@ -893,6 +893,31 @@ impl CStr {
     ///
     /// [`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 �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())