@@ -102,8 +102,8 @@ use sys;
102
102
/// }
103
103
///
104
104
/// // We are certain that our string doesn't have 0 bytes in the middle,
105
- /// // so we can .unwrap ()
106
- /// let c_to_print = CString::new("Hello, world!").unwrap( );
105
+ /// // so we can .expect ()
106
+ /// let c_to_print = CString::new("Hello, world!").expect("CString::new failed" );
107
107
/// unsafe {
108
108
/// my_printer(c_to_print.as_ptr());
109
109
/// }
@@ -175,7 +175,7 @@ pub struct CString {
175
175
/// unsafe { work_with(data.as_ptr()) }
176
176
/// }
177
177
///
178
- /// let s = CString::new("data data data data").unwrap( );
178
+ /// let s = CString::new("data data data data").expect("CString::new failed" );
179
179
/// work(&s);
180
180
/// ```
181
181
///
@@ -314,7 +314,7 @@ impl CString {
314
314
///
315
315
/// extern { fn puts(s: *const c_char); }
316
316
///
317
- /// let to_print = CString::new("Hello!").unwrap( );
317
+ /// let to_print = CString::new("Hello!").expect("CString::new failed" );
318
318
/// unsafe {
319
319
/// puts(to_print.as_ptr());
320
320
/// }
@@ -398,7 +398,7 @@ impl CString {
398
398
/// fn some_extern_function(s: *mut c_char);
399
399
/// }
400
400
///
401
- /// let c_string = CString::new("Hello!").unwrap( );
401
+ /// let c_string = CString::new("Hello!").expect("CString::new failed" );
402
402
/// let raw = c_string.into_raw();
403
403
/// unsafe {
404
404
/// some_extern_function(raw);
@@ -428,7 +428,7 @@ impl CString {
428
428
/// ```
429
429
/// use std::ffi::CString;
430
430
///
431
- /// let c_string = CString::new("foo").unwrap( );
431
+ /// let c_string = CString::new("foo").expect("CString::new failed" );
432
432
///
433
433
/// let ptr = c_string.into_raw();
434
434
///
@@ -460,12 +460,12 @@ impl CString {
460
460
/// use std::ffi::CString;
461
461
///
462
462
/// let valid_utf8 = vec![b'f', b'o', b'o'];
463
- /// let cstring = CString::new(valid_utf8).unwrap( );
464
- /// assert_eq!(cstring.into_string().unwrap( ), "foo");
463
+ /// let cstring = CString::new(valid_utf8).expect("CString::new failed" );
464
+ /// assert_eq!(cstring.into_string().expect("into_string() call failed" ), "foo");
465
465
///
466
466
/// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
467
- /// let cstring = CString::new(invalid_utf8).unwrap( );
468
- /// let err = cstring.into_string().err().unwrap( );
467
+ /// let cstring = CString::new(invalid_utf8).expect("CString::new failed" );
468
+ /// let err = cstring.into_string().err().expect("into_string().err() failed" );
469
469
/// assert_eq!(err.utf8_error().valid_up_to(), 1);
470
470
/// ```
471
471
@@ -489,7 +489,7 @@ impl CString {
489
489
/// ```
490
490
/// use std::ffi::CString;
491
491
///
492
- /// let c_string = CString::new("foo").unwrap( );
492
+ /// let c_string = CString::new("foo").expect("CString::new failed" );
493
493
/// let bytes = c_string.into_bytes();
494
494
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
495
495
/// ```
@@ -511,7 +511,7 @@ impl CString {
511
511
/// ```
512
512
/// use std::ffi::CString;
513
513
///
514
- /// let c_string = CString::new("foo").unwrap( );
514
+ /// let c_string = CString::new("foo").expect("CString::new failed" );
515
515
/// let bytes = c_string.into_bytes_with_nul();
516
516
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
517
517
/// ```
@@ -534,7 +534,7 @@ impl CString {
534
534
/// ```
535
535
/// use std::ffi::CString;
536
536
///
537
- /// let c_string = CString::new("foo").unwrap( );
537
+ /// let c_string = CString::new("foo").expect("CString::new failed" );
538
538
/// let bytes = c_string.as_bytes();
539
539
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
540
540
/// ```
@@ -554,7 +554,7 @@ impl CString {
554
554
/// ```
555
555
/// use std::ffi::CString;
556
556
///
557
- /// let c_string = CString::new("foo").unwrap( );
557
+ /// let c_string = CString::new("foo").expect("CString::new failed" );
558
558
/// let bytes = c_string.as_bytes_with_nul();
559
559
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
560
560
/// ```
@@ -573,9 +573,10 @@ impl CString {
573
573
/// ```
574
574
/// use std::ffi::{CString, CStr};
575
575
///
576
- /// let c_string = CString::new(b"foo".to_vec()).unwrap( );
576
+ /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed" );
577
577
/// let c_str = c_string.as_c_str();
578
- /// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap());
578
+ /// assert_eq!(c_str,
579
+ /// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
579
580
/// ```
580
581
#[ inline]
581
582
#[ stable( feature = "as_c_str" , since = "1.20.0" ) ]
@@ -592,16 +593,17 @@ impl CString {
592
593
/// ```
593
594
/// use std::ffi::{CString, CStr};
594
595
///
595
- /// let c_string = CString::new(b"foo".to_vec()).unwrap( );
596
+ /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed" );
596
597
/// let boxed = c_string.into_boxed_c_str();
597
- /// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap());
598
+ /// assert_eq!(&*boxed,
599
+ /// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
598
600
/// ```
599
601
#[ stable( feature = "into_boxed_c_str" , since = "1.20.0" ) ]
600
602
pub fn into_boxed_c_str ( self ) -> Box < CStr > {
601
603
unsafe { Box :: from_raw ( Box :: into_raw ( self . into_inner ( ) ) as * mut CStr ) }
602
604
}
603
605
604
- // Bypass "move out of struct which implements [`Drop`] trait" restriction.
606
+ /// Bypass "move out of struct which implements [`Drop`] trait" restriction.
605
607
///
606
608
/// [`Drop`]: ../ops/trait.Drop.html
607
609
fn into_inner ( self ) -> Box < [ u8 ] > {
@@ -1031,7 +1033,7 @@ impl CStr {
1031
1033
/// use std::ffi::{CStr, CString};
1032
1034
///
1033
1035
/// unsafe {
1034
- /// let cstring = CString::new("hello").unwrap( );
1036
+ /// let cstring = CString::new("hello").expect("CString::new failed" );
1035
1037
/// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
1036
1038
/// assert_eq!(cstr, &*cstring);
1037
1039
/// }
@@ -1058,7 +1060,7 @@ impl CStr {
1058
1060
/// # #![allow(unused_must_use)]
1059
1061
/// use std::ffi::{CString};
1060
1062
///
1061
- /// let ptr = CString::new("Hello").unwrap( ).as_ptr();
1063
+ /// let ptr = CString::new("Hello").expect("CString::new failed" ).as_ptr();
1062
1064
/// unsafe {
1063
1065
/// // `ptr` is dangling
1064
1066
/// *ptr;
@@ -1067,14 +1069,14 @@ impl CStr {
1067
1069
///
1068
1070
/// This happens because the pointer returned by `as_ptr` does not carry any
1069
1071
/// lifetime information and the [`CString`] is deallocated immediately after
1070
- /// the `CString::new("Hello").unwrap( ).as_ptr()` expression is evaluated.
1072
+ /// the `CString::new("Hello").expect("CString::new failed" ).as_ptr()` expression is evaluated.
1071
1073
/// To fix the problem, bind the `CString` to a local variable:
1072
1074
///
1073
1075
/// ```no_run
1074
1076
/// # #![allow(unused_must_use)]
1075
1077
/// use std::ffi::{CString};
1076
1078
///
1077
- /// let hello = CString::new("Hello").unwrap( );
1079
+ /// let hello = CString::new("Hello").expect("CString::new failed" );
1078
1080
/// let ptr = hello.as_ptr();
1079
1081
/// unsafe {
1080
1082
/// // `ptr` is valid because `hello` is in scope
@@ -1106,7 +1108,7 @@ impl CStr {
1106
1108
/// ```
1107
1109
/// use std::ffi::CStr;
1108
1110
///
1109
- /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap( );
1111
+ /// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed" );
1110
1112
/// assert_eq!(c_str.to_bytes(), b"foo");
1111
1113
/// ```
1112
1114
#[ inline]
@@ -1132,7 +1134,7 @@ impl CStr {
1132
1134
/// ```
1133
1135
/// use std::ffi::CStr;
1134
1136
///
1135
- /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap( );
1137
+ /// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed" );
1136
1138
/// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
1137
1139
/// ```
1138
1140
#[ inline]
@@ -1159,7 +1161,7 @@ impl CStr {
1159
1161
/// ```
1160
1162
/// use std::ffi::CStr;
1161
1163
///
1162
- /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap( );
1164
+ /// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed" );
1163
1165
/// assert_eq!(c_str.to_str(), Ok("foo"));
1164
1166
/// ```
1165
1167
#[ stable( feature = "cstr_to_str" , since = "1.4.0" ) ]
@@ -1200,7 +1202,8 @@ impl CStr {
1200
1202
/// use std::borrow::Cow;
1201
1203
/// use std::ffi::CStr;
1202
1204
///
1203
- /// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
1205
+ /// let c_str = CStr::from_bytes_with_nul(b"Hello World\0")
1206
+ /// .expect("CStr::from_bytes_with_nul failed");
1204
1207
/// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
1205
1208
/// ```
1206
1209
///
@@ -1210,7 +1213,8 @@ impl CStr {
1210
1213
/// use std::borrow::Cow;
1211
1214
/// use std::ffi::CStr;
1212
1215
///
1213
- /// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
1216
+ /// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
1217
+ /// .expect("CStr::from_bytes_with_nul failed");
1214
1218
/// assert_eq!(
1215
1219
/// c_str.to_string_lossy(),
1216
1220
/// Cow::Owned(String::from("Hello �World")) as Cow<str>
@@ -1231,9 +1235,9 @@ impl CStr {
1231
1235
/// ```
1232
1236
/// use std::ffi::CString;
1233
1237
///
1234
- /// let c_string = CString::new(b"foo".to_vec()).unwrap( );
1238
+ /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed" );
1235
1239
/// let boxed = c_string.into_boxed_c_str();
1236
- /// assert_eq!(boxed.into_c_string(), CString::new("foo").unwrap( ));
1240
+ /// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed" ));
1237
1241
/// ```
1238
1242
#[ stable( feature = "into_boxed_c_str" , since = "1.20.0" ) ]
1239
1243
pub fn into_c_string ( self : Box < CStr > ) -> CString {
0 commit comments