2 use core::clone::Clone;
4 use core::ops::DerefMut;
10 let x: Box<_> = Box::new(0);
11 let addr_x: *const isize = mem::transmute(&*x);
14 let addr_y: *const isize = mem::transmute(&*y);
15 assert_eq!(addr_x, addr_y);
21 let x = "test".to_string();
22 let addr_x = x.as_ptr();
25 let addr_y = y.as_ptr();
26 assert_eq!(addr_x, addr_y);
30 fn test_get_resource() {
31 use core::cell::RefCell;
35 i: Rc<RefCell<isize>>,
42 *ii.borrow_mut() = i + 1;
46 fn r(i: Rc<RefCell<isize>>) -> R {
50 let i = Rc::new(RefCell::new(0));
54 let _y = opt.unwrap();
56 assert_eq!(*i.borrow(), 1);
60 #[cfg_attr(not(bootstrap), allow(for_loops_over_fallibles))]
61 fn test_option_dance() {
66 y2 = y.take().unwrap();
74 fn test_option_too_much_dance() {
77 let _y2 = y.take().unwrap();
78 let _y3 = y.take().unwrap();
83 let x: Option<isize> = Some(1);
84 assert_eq!(x.and(Some(2)), Some(2));
85 assert_eq!(x.and(None::<isize>), None);
87 let x: Option<isize> = None;
88 assert_eq!(x.and(Some(2)), None);
89 assert_eq!(x.and(None::<isize>), None);
91 const FOO: Option<isize> = Some(1);
92 const A: Option<isize> = FOO.and(Some(2));
93 const B: Option<isize> = FOO.and(None);
94 assert_eq!(A, Some(2));
97 const BAR: Option<isize> = None;
98 const C: Option<isize> = BAR.and(Some(2));
99 const D: Option<isize> = BAR.and(None);
106 const fn plus_one(x: isize) -> Option<isize> {
110 const fn none(_: isize) -> Option<isize> {
114 let x: Option<isize> = Some(1);
115 assert_eq!(x.and_then(plus_one), Some(2));
116 assert_eq!(x.and_then(none), None);
118 let x: Option<isize> = None;
119 assert_eq!(x.and_then(plus_one), None);
120 assert_eq!(x.and_then(none), None);
122 const FOO: Option<isize> = Some(1);
123 const A: Option<isize> = FOO.and_then(plus_one);
124 const B: Option<isize> = FOO.and_then(none);
125 assert_eq!(A, Some(2));
128 const BAR: Option<isize> = None;
129 const C: Option<isize> = BAR.and_then(plus_one);
130 const D: Option<isize> = BAR.and_then(none);
137 let x: Option<isize> = Some(1);
138 assert_eq!(x.or(Some(2)), Some(1));
139 assert_eq!(x.or(None), Some(1));
141 let x: Option<isize> = None;
142 assert_eq!(x.or(Some(2)), Some(2));
143 assert_eq!(x.or(None), None);
145 const FOO: Option<isize> = Some(1);
146 const A: Option<isize> = FOO.or(Some(2));
147 const B: Option<isize> = FOO.or(None);
148 assert_eq!(A, Some(1));
149 assert_eq!(B, Some(1));
151 const BAR: Option<isize> = None;
152 const C: Option<isize> = BAR.or(Some(2));
153 const D: Option<isize> = BAR.or(None);
154 assert_eq!(C, Some(2));
160 const fn two() -> Option<isize> {
164 const fn none() -> Option<isize> {
168 let x: Option<isize> = Some(1);
169 assert_eq!(x.or_else(two), Some(1));
170 assert_eq!(x.or_else(none), Some(1));
172 let x: Option<isize> = None;
173 assert_eq!(x.or_else(two), Some(2));
174 assert_eq!(x.or_else(none), None);
176 const FOO: Option<isize> = Some(1);
177 const A: Option<isize> = FOO.or_else(two);
178 const B: Option<isize> = FOO.or_else(none);
179 assert_eq!(A, Some(1));
180 assert_eq!(B, Some(1));
182 const BAR: Option<isize> = None;
183 const C: Option<isize> = BAR.or_else(two);
184 const D: Option<isize> = BAR.or_else(none);
185 assert_eq!(C, Some(2));
191 assert_eq!(Some(1).unwrap(), 1);
192 let s = Some("hello".to_string()).unwrap();
193 assert_eq!(s, "hello");
198 fn test_unwrap_panic1() {
199 let x: Option<isize> = None;
205 fn test_unwrap_panic2() {
206 let x: Option<String> = None;
211 fn test_unwrap_or() {
212 let x: Option<isize> = Some(1);
213 assert_eq!(x.unwrap_or(2), 1);
215 let x: Option<isize> = None;
216 assert_eq!(x.unwrap_or(2), 2);
218 const A: isize = Some(1).unwrap_or(2);
219 const B: isize = None.unwrap_or(2);
225 fn test_unwrap_or_else() {
226 const fn two() -> isize {
230 let x: Option<isize> = Some(1);
231 assert_eq!(x.unwrap_or_else(two), 1);
233 let x: Option<isize> = None;
234 assert_eq!(x.unwrap_or_else(two), 2);
236 const A: isize = Some(1).unwrap_or_else(two);
237 const B: isize = None.unwrap_or_else(two);
243 fn test_unwrap_unchecked() {
244 assert_eq!(unsafe { Some(1).unwrap_unchecked() }, 1);
245 let s = unsafe { Some("hello".to_string()).unwrap_unchecked() };
246 assert_eq!(s, "hello");
254 let mut it = x.iter();
256 assert_eq!(it.size_hint(), (1, Some(1)));
257 assert_eq!(it.next(), Some(&val));
258 assert_eq!(it.size_hint(), (0, Some(0)));
259 assert!(it.next().is_none());
261 let mut it = (&x).into_iter();
262 assert_eq!(it.next(), Some(&val));
270 let mut x = Some(val);
272 let mut it = x.iter_mut();
274 assert_eq!(it.size_hint(), (1, Some(1)));
278 assert_eq!(*interior, val);
281 None => assert!(false),
284 assert_eq!(it.size_hint(), (0, Some(0)));
285 assert!(it.next().is_none());
287 assert_eq!(x, Some(new_val));
289 let mut y = Some(val);
290 let mut it = (&mut y).into_iter();
291 assert_eq!(it.next(), Some(&mut val));
296 let small = Some(1.0f64);
297 let big = Some(5.0f64);
298 let nan = Some(0.0f64 / 0.0);
299 assert!(!(nan < big));
300 assert!(!(nan > big));
301 assert!(small < big);
308 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
309 assert!(v == Some(vec![]));
311 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
312 assert!(v == Some(vec![0, 1, 2]));
314 let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
317 // test that it does not take more elements than it needs
318 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
319 [Box::new(|| Some(())), Box::new(|| None), Box::new(|| panic!())];
321 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
330 let opt_none: Option<&'static u32> = None;
331 let opt_ref = Some(&val);
332 let opt_ref_ref = Some(&val_ref);
335 assert_eq!(opt_none.clone(), None);
336 assert_eq!(opt_none.copied(), None);
338 // Immutable ref works
339 assert_eq!(opt_ref.clone(), Some(&val));
340 assert_eq!(opt_ref.copied(), Some(1));
342 // Double Immutable ref works
343 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
344 assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
345 assert_eq!(opt_ref_ref.copied().copied(), Some(1));
352 let opt_none: Option<&'static u32> = None;
353 let opt_ref = Some(&val);
354 let opt_ref_ref = Some(&val_ref);
357 assert_eq!(opt_none.clone(), None);
358 assert_eq!(opt_none.cloned(), None);
360 // Immutable ref works
361 assert_eq!(opt_ref.clone(), Some(&val));
362 assert_eq!(opt_ref.cloned(), Some(1));
364 // Double Immutable ref works
365 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
366 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
367 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
372 fn try_option_some() -> Option<u8> {
376 assert_eq!(try_option_some(), Some(1));
378 fn try_option_none() -> Option<u8> {
382 assert_eq!(try_option_none(), None);
386 fn test_option_as_deref() {
387 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
388 let ref_option = &Some(&42);
389 assert_eq!(ref_option.as_deref(), Some(&42));
391 let ref_option = &Some(String::from("a result"));
392 assert_eq!(ref_option.as_deref(), Some("a result"));
394 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
395 assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
397 // None: &Option<T: Deref>>::None -> None
398 let ref_option: &Option<&i32> = &None;
399 assert_eq!(ref_option.as_deref(), None);
403 fn test_option_as_deref_mut() {
404 // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
406 let ref_option = &mut Some(&mut val);
407 assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
409 let ref_option = &mut Some(String::from("a result"));
410 assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
412 let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
413 assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
415 // None: &mut Option<T: Deref>>::None -> None
416 let ref_option: &mut Option<&mut i32> = &mut None;
417 assert_eq!(ref_option.as_deref_mut(), None);
423 let old = x.replace(5);
425 assert_eq!(x, Some(5));
426 assert_eq!(old, Some(2));
429 let old = x.replace(3);
431 assert_eq!(x, Some(3));
432 assert_eq!(old, None);
437 // test that the methods of `Option` are usable in a const context
439 const OPTION: Option<usize> = Some(32);
440 assert_eq!(OPTION, Some(32));
442 const OPTION_FROM: Option<usize> = Option::from(32);
443 assert_eq!(OPTION_FROM, Some(32));
445 const REF: Option<&usize> = OPTION.as_ref();
446 assert_eq!(REF, Some(&32));
448 const REF_FROM: Option<&usize> = Option::from(&OPTION);
449 assert_eq!(REF_FROM, Some(&32));
451 const IS_SOME: bool = OPTION.is_some();
454 const IS_NONE: bool = OPTION.is_none();
457 const COPIED: Option<usize> = OPTION.as_ref().copied();
458 assert_eq!(COPIED, OPTION);
462 const fn option_const_mut() {
463 // test that the methods of `Option` that take mutable references are usable in a const context
465 let mut option: Option<usize> = Some(32);
467 let _take = option.take();
468 let _replace = option.replace(42);
471 let as_mut = option.as_mut();
474 None => unreachable!(),
479 let as_mut: Option<&mut usize> = Option::from(&mut option);
482 None => unreachable!(),
488 fn test_unwrap_drop() {
493 impl<'a> std::ops::Drop for Dtor<'a> {
495 self.x.set(self.x.get() - 1);
499 fn unwrap<T>(o: Option<T>) -> T {
506 let x = &Cell::new(1);
509 let b = Some(Dtor { x });
513 assert_eq!(x.get(), 0);
518 let thing = "{{ f }}";
519 let f = thing.find("{{");
530 let z: Option<usize> = None;
532 assert_eq!(x.zip(y), Some((10, "foo")));
533 assert_eq!(x.zip(z), None);
534 assert_eq!(z.zip(x), None);
539 let x = Some((10, "foo"));
540 let y = None::<(bool, i32)>;
542 assert_eq!(x.unzip(), (Some(10), Some("foo")));
543 assert_eq!(y.unzip(), (None, None));
547 fn zip_unzip_roundtrip() {
552 assert_eq!(z, Some((10, "foo")));
555 assert_eq!(a, (x, y));