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 fn test_option_dance() {
65 y2 = y.take().unwrap();
73 fn test_option_too_much_dance() {
76 let _y2 = y.take().unwrap();
77 let _y3 = y.take().unwrap();
82 let x: Option<isize> = Some(1);
83 assert_eq!(x.and(Some(2)), Some(2));
84 assert_eq!(x.and(None::<isize>), None);
86 let x: Option<isize> = None;
87 assert_eq!(x.and(Some(2)), None);
88 assert_eq!(x.and(None::<isize>), None);
90 const FOO: Option<isize> = Some(1);
91 const A: Option<isize> = FOO.and(Some(2));
92 const B: Option<isize> = FOO.and(None);
93 assert_eq!(A, Some(2));
96 const BAR: Option<isize> = None;
97 const C: Option<isize> = BAR.and(Some(2));
98 const D: Option<isize> = BAR.and(None);
105 const fn plus_one(x: isize) -> Option<isize> {
109 const fn none(_: isize) -> Option<isize> {
113 let x: Option<isize> = Some(1);
114 assert_eq!(x.and_then(plus_one), Some(2));
115 assert_eq!(x.and_then(none), None);
117 let x: Option<isize> = None;
118 assert_eq!(x.and_then(plus_one), None);
119 assert_eq!(x.and_then(none), None);
121 const FOO: Option<isize> = Some(1);
122 const A: Option<isize> = FOO.and_then(plus_one);
123 const B: Option<isize> = FOO.and_then(none);
124 assert_eq!(A, Some(2));
127 const BAR: Option<isize> = None;
128 const C: Option<isize> = BAR.and_then(plus_one);
129 const D: Option<isize> = BAR.and_then(none);
136 let x: Option<isize> = Some(1);
137 assert_eq!(x.or(Some(2)), Some(1));
138 assert_eq!(x.or(None), Some(1));
140 let x: Option<isize> = None;
141 assert_eq!(x.or(Some(2)), Some(2));
142 assert_eq!(x.or(None), None);
144 const FOO: Option<isize> = Some(1);
145 const A: Option<isize> = FOO.or(Some(2));
146 const B: Option<isize> = FOO.or(None);
147 assert_eq!(A, Some(1));
148 assert_eq!(B, Some(1));
150 const BAR: Option<isize> = None;
151 const C: Option<isize> = BAR.or(Some(2));
152 const D: Option<isize> = BAR.or(None);
153 assert_eq!(C, Some(2));
159 const fn two() -> Option<isize> {
163 const fn none() -> Option<isize> {
167 let x: Option<isize> = Some(1);
168 assert_eq!(x.or_else(two), Some(1));
169 assert_eq!(x.or_else(none), Some(1));
171 let x: Option<isize> = None;
172 assert_eq!(x.or_else(two), Some(2));
173 assert_eq!(x.or_else(none), None);
175 const FOO: Option<isize> = Some(1);
176 const A: Option<isize> = FOO.or_else(two);
177 const B: Option<isize> = FOO.or_else(none);
178 assert_eq!(A, Some(1));
179 assert_eq!(B, Some(1));
181 const BAR: Option<isize> = None;
182 const C: Option<isize> = BAR.or_else(two);
183 const D: Option<isize> = BAR.or_else(none);
184 assert_eq!(C, Some(2));
190 assert_eq!(Some(1).unwrap(), 1);
191 let s = Some("hello".to_string()).unwrap();
192 assert_eq!(s, "hello");
197 fn test_unwrap_panic1() {
198 let x: Option<isize> = None;
204 fn test_unwrap_panic2() {
205 let x: Option<String> = None;
210 fn test_unwrap_or() {
211 let x: Option<isize> = Some(1);
212 assert_eq!(x.unwrap_or(2), 1);
214 let x: Option<isize> = None;
215 assert_eq!(x.unwrap_or(2), 2);
217 const A: isize = Some(1).unwrap_or(2);
218 const B: isize = None.unwrap_or(2);
224 fn test_unwrap_or_else() {
225 const fn two() -> isize {
229 let x: Option<isize> = Some(1);
230 assert_eq!(x.unwrap_or_else(two), 1);
232 let x: Option<isize> = None;
233 assert_eq!(x.unwrap_or_else(two), 2);
235 const A: isize = Some(1).unwrap_or_else(two);
236 const B: isize = None.unwrap_or_else(two);
242 fn test_unwrap_unchecked() {
243 assert_eq!(unsafe { Some(1).unwrap_unchecked() }, 1);
244 let s = unsafe { Some("hello".to_string()).unwrap_unchecked() };
245 assert_eq!(s, "hello");
253 let mut it = x.iter();
255 assert_eq!(it.size_hint(), (1, Some(1)));
256 assert_eq!(it.next(), Some(&val));
257 assert_eq!(it.size_hint(), (0, Some(0)));
258 assert!(it.next().is_none());
260 let mut it = (&x).into_iter();
261 assert_eq!(it.next(), Some(&val));
269 let mut x = Some(val);
271 let mut it = x.iter_mut();
273 assert_eq!(it.size_hint(), (1, Some(1)));
277 assert_eq!(*interior, val);
280 None => assert!(false),
283 assert_eq!(it.size_hint(), (0, Some(0)));
284 assert!(it.next().is_none());
286 assert_eq!(x, Some(new_val));
288 let mut y = Some(val);
289 let mut it = (&mut y).into_iter();
290 assert_eq!(it.next(), Some(&mut val));
295 let small = Some(1.0f64);
296 let big = Some(5.0f64);
297 let nan = Some(0.0f64 / 0.0);
298 assert!(!(nan < big));
299 assert!(!(nan > big));
300 assert!(small < big);
307 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
308 assert!(v == Some(vec![]));
310 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
311 assert!(v == Some(vec![0, 1, 2]));
313 let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
316 // test that it does not take more elements than it needs
317 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
318 [Box::new(|| Some(())), Box::new(|| None), Box::new(|| panic!())];
320 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
329 let opt_none: Option<&'static u32> = None;
330 let opt_ref = Some(&val);
331 let opt_ref_ref = Some(&val_ref);
334 assert_eq!(opt_none.clone(), None);
335 assert_eq!(opt_none.copied(), None);
337 // Immutable ref works
338 assert_eq!(opt_ref.clone(), Some(&val));
339 assert_eq!(opt_ref.copied(), Some(1));
341 // Double Immutable ref works
342 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
343 assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
344 assert_eq!(opt_ref_ref.copied().copied(), Some(1));
351 let opt_none: Option<&'static u32> = None;
352 let opt_ref = Some(&val);
353 let opt_ref_ref = Some(&val_ref);
356 assert_eq!(opt_none.clone(), None);
357 assert_eq!(opt_none.cloned(), None);
359 // Immutable ref works
360 assert_eq!(opt_ref.clone(), Some(&val));
361 assert_eq!(opt_ref.cloned(), Some(1));
363 // Double Immutable ref works
364 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
365 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
366 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
371 fn try_option_some() -> Option<u8> {
375 assert_eq!(try_option_some(), Some(1));
377 fn try_option_none() -> Option<u8> {
381 assert_eq!(try_option_none(), None);
385 fn test_option_as_deref() {
386 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
387 let ref_option = &Some(&42);
388 assert_eq!(ref_option.as_deref(), Some(&42));
390 let ref_option = &Some(String::from("a result"));
391 assert_eq!(ref_option.as_deref(), Some("a result"));
393 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
394 assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
396 // None: &Option<T: Deref>>::None -> None
397 let ref_option: &Option<&i32> = &None;
398 assert_eq!(ref_option.as_deref(), None);
402 fn test_option_as_deref_mut() {
403 // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
405 let ref_option = &mut Some(&mut val);
406 assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
408 let ref_option = &mut Some(String::from("a result"));
409 assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
411 let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
412 assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
414 // None: &mut Option<T: Deref>>::None -> None
415 let ref_option: &mut Option<&mut i32> = &mut None;
416 assert_eq!(ref_option.as_deref_mut(), None);
422 let old = x.replace(5);
424 assert_eq!(x, Some(5));
425 assert_eq!(old, Some(2));
428 let old = x.replace(3);
430 assert_eq!(x, Some(3));
431 assert_eq!(old, None);
436 // test that the methods of `Option` are usable in a const context
438 const OPTION: Option<usize> = Some(32);
439 assert_eq!(OPTION, Some(32));
441 const OPTION_FROM: Option<usize> = Option::from(32);
442 assert_eq!(OPTION_FROM, Some(32));
444 const REF: Option<&usize> = OPTION.as_ref();
445 assert_eq!(REF, Some(&32));
447 const REF_FROM: Option<&usize> = Option::from(&OPTION);
448 assert_eq!(REF_FROM, Some(&32));
450 const IS_SOME: bool = OPTION.is_some();
453 const IS_NONE: bool = OPTION.is_none();
456 const COPIED: Option<usize> = OPTION.as_ref().copied();
457 assert_eq!(COPIED, OPTION);
461 const fn option_const_mut() {
462 // test that the methods of `Option` that take mutable references are usable in a const context
464 let mut option: Option<usize> = Some(32);
466 let _take = option.take();
467 let _replace = option.replace(42);
470 let as_mut = option.as_mut();
473 None => unreachable!(),
478 let as_mut: Option<&mut usize> = Option::from(&mut option);
481 None => unreachable!(),
487 fn test_unwrap_drop() {
492 impl<'a> std::ops::Drop for Dtor<'a> {
494 self.x.set(self.x.get() - 1);
498 fn unwrap<T>(o: Option<T>) -> T {
505 let x = &Cell::new(1);
508 let b = Some(Dtor { x });
512 assert_eq!(x.get(), 0);
517 let thing = "{{ f }}";
518 let f = thing.find("{{");
529 let z: Option<usize> = None;
531 assert_eq!(x.zip(y), Some((10, "foo")));
532 assert_eq!(x.zip(z), None);
533 assert_eq!(z.zip(x), None);
538 let x = Some((10, "foo"));
539 let y = None::<(bool, i32)>;
541 assert_eq!(x.unzip(), (Some(10), Some("foo")));
542 assert_eq!(y.unzip(), (None, None));
546 fn zip_unzip_roundtrip() {
551 assert_eq!(z, Some((10, "foo")));
554 assert_eq!(a, (x, y));