File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -893,6 +893,31 @@ impl CStr {
893
893
///
894
894
/// [`Cow`]: ../borrow/enum.Cow.html
895
895
/// [`str`]: ../primitive.str.html
896
+ ///
897
+ /// # Examples
898
+ ///
899
+ /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
900
+ ///
901
+ /// ```
902
+ /// use std::borrow::Cow;
903
+ /// use std::ffi::CStr;
904
+ ///
905
+ /// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
906
+ /// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
907
+ /// ```
908
+ ///
909
+ /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
910
+ ///
911
+ /// ```
912
+ /// use std::borrow::Cow;
913
+ /// use std::ffi::CStr;
914
+ ///
915
+ /// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
916
+ /// assert_eq!(
917
+ /// c_str.to_string_lossy(),
918
+ /// Cow::Owned(String::from("Hello �World")) as Cow<str>
919
+ /// );
920
+ /// ```
896
921
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
897
922
pub fn to_string_lossy ( & self ) -> Cow < str > {
898
923
String :: from_utf8_lossy ( self . to_bytes ( ) )
You can’t perform that action at this time.
0 commit comments