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