Skip to content

Commit a747842

Browse files
Replace unwrap calls in example by expect
1 parent 0aa8d03 commit a747842

28 files changed

+345
-316
lines changed

src/libstd/collections/hash/set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,9 @@ impl<T, S> HashSet<T, S>
678678
/// let mut set = HashSet::new();
679679
/// set.insert(Vec::<i32>::new());
680680
///
681-
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
681+
/// assert_eq!(set.get(&[][..]).expect("get() failed").capacity(), 0);
682682
/// set.replace(Vec::with_capacity(10));
683-
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
683+
/// assert_eq!(set.get(&[][..]).expect("get() failed").capacity(), 10);
684684
/// ```
685685
#[stable(feature = "set_recovery", since = "1.9.0")]
686686
pub fn replace(&mut self, value: T) -> Option<T> {

src/libstd/collections/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,10 @@
401401
//! map.insert(Foo { a: 1, b: "xyz" }, 100);
402402
//!
403403
//! // The value has been updated...
404-
//! assert_eq!(map.values().next().unwrap(), &100);
404+
//! assert_eq!(map.values().next().expect("empty value: we reached the end"), &100);
405405
//!
406406
//! // ...but the key hasn't changed. b is still "baz", not "xyz".
407-
//! assert_eq!(map.keys().next().unwrap().b, "baz");
407+
//! assert_eq!(map.keys().next().expect("empty value: we reached the end").b, "baz");
408408
//! ```
409409
//!
410410
//! [`Vec`]: ../../std/vec/struct.Vec.html

src/libstd/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub trait Error: Debug + Display {
131131
/// match get_super_error() {
132132
/// Err(e) => {
133133
/// println!("Error: {}", e.description());
134-
/// println!("Caused by: {}", e.cause().unwrap());
134+
/// println!("Caused by: {}", e.cause().expect("no cause provided"));
135135
/// }
136136
/// _ => println!("No error"),
137137
/// }

src/libstd/ffi/c_str.rs

+35-31
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ use sys;
101101
/// }
102102
///
103103
/// // 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");
106106
/// unsafe {
107107
/// my_printer(c_to_print.as_ptr());
108108
/// }
@@ -174,7 +174,7 @@ pub struct CString {
174174
/// unsafe { work_with(data.as_ptr()) }
175175
/// }
176176
///
177-
/// let s = CString::new("data data data data").unwrap();
177+
/// let s = CString::new("data data data data").expect("CString::new failed");
178178
/// work(&s);
179179
/// ```
180180
///
@@ -313,7 +313,7 @@ impl CString {
313313
///
314314
/// extern { fn puts(s: *const c_char); }
315315
///
316-
/// let to_print = CString::new("Hello!").unwrap();
316+
/// let to_print = CString::new("Hello!").expect("CString::new failed");
317317
/// unsafe {
318318
/// puts(to_print.as_ptr());
319319
/// }
@@ -397,7 +397,7 @@ impl CString {
397397
/// fn some_extern_function(s: *mut c_char);
398398
/// }
399399
///
400-
/// let c_string = CString::new("Hello!").unwrap();
400+
/// let c_string = CString::new("Hello!").expect("CString::new failed");
401401
/// let raw = c_string.into_raw();
402402
/// unsafe {
403403
/// some_extern_function(raw);
@@ -427,7 +427,7 @@ impl CString {
427427
/// ```
428428
/// use std::ffi::CString;
429429
///
430-
/// let c_string = CString::new("foo").unwrap();
430+
/// let c_string = CString::new("foo").expect("CString::new failed");
431431
///
432432
/// let ptr = c_string.into_raw();
433433
///
@@ -459,12 +459,12 @@ impl CString {
459459
/// use std::ffi::CString;
460460
///
461461
/// 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");
464464
///
465465
/// 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");
468468
/// assert_eq!(err.utf8_error().valid_up_to(), 1);
469469
/// ```
470470
@@ -488,7 +488,7 @@ impl CString {
488488
/// ```
489489
/// use std::ffi::CString;
490490
///
491-
/// let c_string = CString::new("foo").unwrap();
491+
/// let c_string = CString::new("foo").expect("CString::new failed");
492492
/// let bytes = c_string.into_bytes();
493493
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
494494
/// ```
@@ -510,7 +510,7 @@ impl CString {
510510
/// ```
511511
/// use std::ffi::CString;
512512
///
513-
/// let c_string = CString::new("foo").unwrap();
513+
/// let c_string = CString::new("foo").expect("CString::new failed");
514514
/// let bytes = c_string.into_bytes_with_nul();
515515
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
516516
/// ```
@@ -533,7 +533,7 @@ impl CString {
533533
/// ```
534534
/// use std::ffi::CString;
535535
///
536-
/// let c_string = CString::new("foo").unwrap();
536+
/// let c_string = CString::new("foo").expect("CString::new failed");
537537
/// let bytes = c_string.as_bytes();
538538
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
539539
/// ```
@@ -553,7 +553,7 @@ impl CString {
553553
/// ```
554554
/// use std::ffi::CString;
555555
///
556-
/// let c_string = CString::new("foo").unwrap();
556+
/// let c_string = CString::new("foo").expect("CString::new failed");
557557
/// let bytes = c_string.as_bytes_with_nul();
558558
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
559559
/// ```
@@ -572,9 +572,10 @@ impl CString {
572572
/// ```
573573
/// use std::ffi::{CString, CStr};
574574
///
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");
576576
/// 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"));
578579
/// ```
579580
#[inline]
580581
#[stable(feature = "as_c_str", since = "1.20.0")]
@@ -591,16 +592,17 @@ impl CString {
591592
/// ```
592593
/// use std::ffi::{CString, CStr};
593594
///
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");
595596
/// 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"));
597599
/// ```
598600
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
599601
pub fn into_boxed_c_str(self) -> Box<CStr> {
600602
unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
601603
}
602604

603-
// Bypass "move out of struct which implements [`Drop`] trait" restriction.
605+
/// Bypass "move out of struct which implements [`Drop`] trait" restriction.
604606
///
605607
/// [`Drop`]: ../ops/trait.Drop.html
606608
fn into_inner(self) -> Box<[u8]> {
@@ -961,7 +963,7 @@ impl CStr {
961963
///
962964
/// unsafe {
963965
/// 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"));
965967
/// }
966968
/// # }
967969
/// ```
@@ -1030,7 +1032,7 @@ impl CStr {
10301032
/// use std::ffi::{CStr, CString};
10311033
///
10321034
/// unsafe {
1033-
/// let cstring = CString::new("hello").unwrap();
1035+
/// let cstring = CString::new("hello").expect("CString::new failed");
10341036
/// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
10351037
/// assert_eq!(cstr, &*cstring);
10361038
/// }
@@ -1057,7 +1059,7 @@ impl CStr {
10571059
/// # #![allow(unused_must_use)]
10581060
/// use std::ffi::{CString};
10591061
///
1060-
/// let ptr = CString::new("Hello").unwrap().as_ptr();
1062+
/// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
10611063
/// unsafe {
10621064
/// // `ptr` is dangling
10631065
/// *ptr;
@@ -1066,14 +1068,14 @@ impl CStr {
10661068
///
10671069
/// This happens because the pointer returned by `as_ptr` does not carry any
10681070
/// 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.
10701072
/// To fix the problem, bind the `CString` to a local variable:
10711073
///
10721074
/// ```no_run
10731075
/// # #![allow(unused_must_use)]
10741076
/// use std::ffi::{CString};
10751077
///
1076-
/// let hello = CString::new("Hello").unwrap();
1078+
/// let hello = CString::new("Hello").expect("CString::new failed");
10771079
/// let ptr = hello.as_ptr();
10781080
/// unsafe {
10791081
/// // `ptr` is valid because `hello` is in scope
@@ -1105,7 +1107,7 @@ impl CStr {
11051107
/// ```
11061108
/// use std::ffi::CStr;
11071109
///
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");
11091111
/// assert_eq!(c_str.to_bytes(), b"foo");
11101112
/// ```
11111113
#[inline]
@@ -1131,7 +1133,7 @@ impl CStr {
11311133
/// ```
11321134
/// use std::ffi::CStr;
11331135
///
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");
11351137
/// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
11361138
/// ```
11371139
#[inline]
@@ -1158,7 +1160,7 @@ impl CStr {
11581160
/// ```
11591161
/// use std::ffi::CStr;
11601162
///
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");
11621164
/// assert_eq!(c_str.to_str(), Ok("foo"));
11631165
/// ```
11641166
#[stable(feature = "cstr_to_str", since = "1.4.0")]
@@ -1198,7 +1200,8 @@ impl CStr {
11981200
/// use std::borrow::Cow;
11991201
/// use std::ffi::CStr;
12001202
///
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");
12021205
/// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
12031206
/// ```
12041207
///
@@ -1208,7 +1211,8 @@ impl CStr {
12081211
/// use std::borrow::Cow;
12091212
/// use std::ffi::CStr;
12101213
///
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");
12121216
/// assert_eq!(
12131217
/// c_str.to_string_lossy(),
12141218
/// Cow::Owned(String::from("Hello �World")) as Cow<str>
@@ -1229,9 +1233,9 @@ impl CStr {
12291233
/// ```
12301234
/// use std::ffi::CString;
12311235
///
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");
12331237
/// 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"));
12351239
/// ```
12361240
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
12371241
pub fn into_c_string(self: Box<CStr>) -> CString {

src/libstd/fs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2023,9 +2023,10 @@ impl DirBuilder {
20232023
/// let path = "/tmp/foo/bar/baz";
20242024
/// DirBuilder::new()
20252025
/// .recursive(true)
2026-
/// .create(path).unwrap();
2026+
/// .create(path)
2027+
/// .expect("DirBuilder creation failed");
20272028
///
2028-
/// assert!(fs::metadata(path).unwrap().is_dir());
2029+
/// assert!(fs::metadata(path).expect("fs::metadata call failed").is_dir());
20292030
/// ```
20302031
#[stable(feature = "dir_builder", since = "1.6.0")]
20312032
pub fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {

0 commit comments

Comments
 (0)