@@ -152,6 +152,14 @@ pub struct CStr {
152
152
/// in the vector provided.
153
153
///
154
154
/// [`CString::new`]: struct.CString.html#method.new
155
+ ///
156
+ /// # Examples
157
+ ///
158
+ /// ```
159
+ /// use std::ffi::{CString, NulError};
160
+ ///
161
+ /// let _: NulError = CString::new(b"f\0oo".to_vec()).unwrap_err();
162
+ /// ```
155
163
#[ derive( Clone , PartialEq , Eq , Debug ) ]
156
164
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
157
165
pub struct NulError ( usize , Vec < u8 > ) ;
@@ -160,6 +168,14 @@ pub struct NulError(usize, Vec<u8>);
160
168
/// byte was found too early in the slice provided or one wasn't found at all.
161
169
///
162
170
/// [`CStr::from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
171
+ ///
172
+ /// # Examples
173
+ ///
174
+ /// ```
175
+ /// use std::ffi::{CStr, FromBytesWithNulError};
176
+ ///
177
+ /// let _: FromBytesWithNulError = CStr::from_bytes_with_nul(b"f\0oo").unwrap_err();
178
+ /// ```
163
179
#[ derive( Clone , PartialEq , Eq , Debug ) ]
164
180
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
165
181
pub struct FromBytesWithNulError {
@@ -271,6 +287,27 @@ impl CString {
271
287
/// to undefined behavior or allocator corruption.
272
288
///
273
289
/// [`into_raw`]: #method.into_raw
290
+ ///
291
+ /// # Examples
292
+ ///
293
+ /// Create a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
294
+ /// ownership with `from_raw`:
295
+ ///
296
+ /// ```no_run
297
+ /// use std::ffi::CString;
298
+ /// use std::os::raw::c_char;
299
+ ///
300
+ /// extern {
301
+ /// fn some_extern_function(s: *mut c_char);
302
+ /// }
303
+ ///
304
+ /// let c_string = CString::new("Hello!").unwrap();
305
+ /// let raw = c_string.into_raw();
306
+ /// unsafe {
307
+ /// some_extern_function(raw);
308
+ /// let c_string = CString::from_raw(raw);
309
+ /// }
310
+ /// ```
274
311
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
275
312
pub unsafe fn from_raw ( ptr : * mut c_char ) -> CString {
276
313
let len = libc:: strlen ( ptr) + 1 ; // Including the NUL byte
@@ -412,6 +449,18 @@ impl CString {
412
449
/// Extracts a [`CStr`] slice containing the entire string.
413
450
///
414
451
/// [`CStr`]: struct.CStr.html
452
+ ///
453
+ /// # Examples
454
+ ///
455
+ /// ```
456
+ /// #![feature(as_c_str)]
457
+ ///
458
+ /// use std::ffi::{CString, CStr};
459
+ ///
460
+ /// let c_string = CString::new(b"foo".to_vec()).unwrap();
461
+ /// let c_str = c_string.as_c_str();
462
+ /// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap());
463
+ /// ```
415
464
#[ inline]
416
465
#[ unstable( feature = "as_c_str" , issue = "40380" ) ]
417
466
pub fn as_c_str ( & self ) -> & CStr {
@@ -421,6 +470,18 @@ impl CString {
421
470
/// Converts this `CString` into a boxed [`CStr`].
422
471
///
423
472
/// [`CStr`]: struct.CStr.html
473
+ ///
474
+ /// # Examples
475
+ ///
476
+ /// ```
477
+ /// #![feature(into_boxed_c_str)]
478
+ ///
479
+ /// use std::ffi::{CString, CStr};
480
+ ///
481
+ /// let c_string = CString::new(b"foo".to_vec()).unwrap();
482
+ /// let boxed = c_string.into_boxed_c_str();
483
+ /// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap());
484
+ /// ```
424
485
#[ unstable( feature = "into_boxed_c_str" , issue = "40380" ) ]
425
486
pub fn into_boxed_c_str ( self ) -> Box < CStr > {
426
487
unsafe { mem:: transmute ( self . into_inner ( ) ) }
@@ -708,6 +769,24 @@ impl CStr {
708
769
/// let cstr = CStr::from_bytes_with_nul(b"hello\0");
709
770
/// assert!(cstr.is_ok());
710
771
/// ```
772
+ ///
773
+ /// Creating a `CStr` without a trailing nul byte is an error:
774
+ ///
775
+ /// ```
776
+ /// use std::ffi::CStr;
777
+ ///
778
+ /// let c_str = CStr::from_bytes_with_nul(b"hello");
779
+ /// assert!(c_str.is_err());
780
+ /// ```
781
+ ///
782
+ /// Creating a `CStr` with an interior nul byte is an error:
783
+ ///
784
+ /// ```
785
+ /// use std::ffi::CStr;
786
+ ///
787
+ /// let c_str = CStr::from_bytes_with_nul(b"he\0llo\0");
788
+ /// assert!(c_str.is_err());
789
+ /// ```
711
790
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
712
791
pub fn from_bytes_with_nul ( bytes : & [ u8 ] )
713
792
-> Result < & CStr , FromBytesWithNulError > {
@@ -800,6 +879,15 @@ impl CStr {
800
879
/// > **Note**: This method is currently implemented as a 0-cost cast, but
801
880
/// > it is planned to alter its definition in the future to perform the
802
881
/// > length calculation whenever this method is called.
882
+ ///
883
+ /// # Examples
884
+ ///
885
+ /// ```
886
+ /// use std::ffi::CStr;
887
+ ///
888
+ /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
889
+ /// assert_eq!(c_str.to_bytes(), b"foo");
890
+ /// ```
803
891
#[ inline]
804
892
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
805
893
pub fn to_bytes ( & self ) -> & [ u8 ] {
@@ -817,6 +905,15 @@ impl CStr {
817
905
/// > length calculation whenever this method is called.
818
906
///
819
907
/// [`to_bytes`]: #method.to_bytes
908
+ ///
909
+ /// # Examples
910
+ ///
911
+ /// ```
912
+ /// use std::ffi::CStr;
913
+ ///
914
+ /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
915
+ /// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
916
+ /// ```
820
917
#[ inline]
821
918
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
822
919
pub fn to_bytes_with_nul ( & self ) -> & [ u8 ] {
@@ -834,6 +931,15 @@ impl CStr {
834
931
/// > check whenever this method is called.
835
932
///
836
933
/// [`&str`]: ../primitive.str.html
934
+ ///
935
+ /// # Examples
936
+ ///
937
+ /// ```
938
+ /// use std::ffi::CStr;
939
+ ///
940
+ /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
941
+ /// assert_eq!(c_str.to_str(), Ok("foo"));
942
+ /// ```
837
943
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
838
944
pub fn to_str ( & self ) -> Result < & str , str:: Utf8Error > {
839
945
// NB: When CStr is changed to perform the length check in .to_bytes()
@@ -857,6 +963,31 @@ impl CStr {
857
963
///
858
964
/// [`Cow`]: ../borrow/enum.Cow.html
859
965
/// [`str`]: ../primitive.str.html
966
+ ///
967
+ /// # Examples
968
+ ///
969
+ /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
970
+ ///
971
+ /// ```
972
+ /// use std::borrow::Cow;
973
+ /// use std::ffi::CStr;
974
+ ///
975
+ /// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
976
+ /// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
977
+ /// ```
978
+ ///
979
+ /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
980
+ ///
981
+ /// ```
982
+ /// use std::borrow::Cow;
983
+ /// use std::ffi::CStr;
984
+ ///
985
+ /// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
986
+ /// assert_eq!(
987
+ /// c_str.to_string_lossy(),
988
+ /// Cow::Owned(String::from("Hello �World")) as Cow<str>
989
+ /// );
990
+ /// ```
860
991
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
861
992
pub fn to_string_lossy ( & self ) -> Cow < str > {
862
993
String :: from_utf8_lossy ( self . to_bytes ( ) )
@@ -866,6 +997,18 @@ impl CStr {
866
997
///
867
998
/// [`Box`]: ../boxed/struct.Box.html
868
999
/// [`CString`]: struct.CString.html
1000
+ ///
1001
+ /// # Examples
1002
+ ///
1003
+ /// ```
1004
+ /// #![feature(into_boxed_c_str)]
1005
+ ///
1006
+ /// use std::ffi::CString;
1007
+ ///
1008
+ /// let c_string = CString::new(b"foo".to_vec()).unwrap();
1009
+ /// let boxed = c_string.into_boxed_c_str();
1010
+ /// assert_eq!(boxed.into_c_string(), CString::new("foo").unwrap());
1011
+ /// ```
869
1012
#[ unstable( feature = "into_boxed_c_str" , issue = "40380" ) ]
870
1013
pub fn into_c_string ( self : Box < CStr > ) -> CString {
871
1014
unsafe { mem:: transmute ( self ) }
0 commit comments