2 use core::clone::Clone;
4 use core::ops::DerefMut;
10 let x: Box<_> = box 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);
93 let x: Option<isize> = Some(1);
94 assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
95 assert_eq!(x.and_then(|_| None::<isize>), None);
97 let x: Option<isize> = None;
98 assert_eq!(x.and_then(|x| Some(x + 1)), None);
99 assert_eq!(x.and_then(|_| None::<isize>), None);
104 let x: Option<isize> = Some(1);
105 assert_eq!(x.or(Some(2)), Some(1));
106 assert_eq!(x.or(None), Some(1));
108 let x: Option<isize> = None;
109 assert_eq!(x.or(Some(2)), Some(2));
110 assert_eq!(x.or(None), None);
115 let x: Option<isize> = Some(1);
116 assert_eq!(x.or_else(|| Some(2)), Some(1));
117 assert_eq!(x.or_else(|| None), Some(1));
119 let x: Option<isize> = None;
120 assert_eq!(x.or_else(|| Some(2)), Some(2));
121 assert_eq!(x.or_else(|| None), None);
126 assert_eq!(Some(1).unwrap(), 1);
127 let s = Some("hello".to_string()).unwrap();
128 assert_eq!(s, "hello");
133 fn test_unwrap_panic1() {
134 let x: Option<isize> = None;
140 fn test_unwrap_panic2() {
141 let x: Option<String> = None;
146 fn test_unwrap_or() {
147 let x: Option<isize> = Some(1);
148 assert_eq!(x.unwrap_or(2), 1);
150 let x: Option<isize> = None;
151 assert_eq!(x.unwrap_or(2), 2);
155 fn test_unwrap_or_else() {
156 let x: Option<isize> = Some(1);
157 assert_eq!(x.unwrap_or_else(|| 2), 1);
159 let x: Option<isize> = None;
160 assert_eq!(x.unwrap_or_else(|| 2), 2);
164 fn test_unwrap_unchecked() {
165 assert_eq!(unsafe { Some(1).unwrap_unchecked() }, 1);
166 let s = unsafe { Some("hello".to_string()).unwrap_unchecked() };
167 assert_eq!(s, "hello");
175 let mut it = x.iter();
177 assert_eq!(it.size_hint(), (1, Some(1)));
178 assert_eq!(it.next(), Some(&val));
179 assert_eq!(it.size_hint(), (0, Some(0)));
180 assert!(it.next().is_none());
182 let mut it = (&x).into_iter();
183 assert_eq!(it.next(), Some(&val));
191 let mut x = Some(val);
193 let mut it = x.iter_mut();
195 assert_eq!(it.size_hint(), (1, Some(1)));
199 assert_eq!(*interior, val);
202 None => assert!(false),
205 assert_eq!(it.size_hint(), (0, Some(0)));
206 assert!(it.next().is_none());
208 assert_eq!(x, Some(new_val));
210 let mut y = Some(val);
211 let mut it = (&mut y).into_iter();
212 assert_eq!(it.next(), Some(&mut val));
217 let small = Some(1.0f64);
218 let big = Some(5.0f64);
219 let nan = Some(0.0f64 / 0.0);
220 assert!(!(nan < big));
221 assert!(!(nan > big));
222 assert!(small < big);
229 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
230 assert!(v == Some(vec![]));
232 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
233 assert!(v == Some(vec![0, 1, 2]));
235 let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
238 // test that it does not take more elements than it needs
239 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
240 [box || Some(()), box || None, box || panic!()];
242 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
251 let opt_none: Option<&'static u32> = None;
252 let opt_ref = Some(&val);
253 let opt_ref_ref = Some(&val_ref);
256 assert_eq!(opt_none.clone(), None);
257 assert_eq!(opt_none.copied(), None);
259 // Immutable ref works
260 assert_eq!(opt_ref.clone(), Some(&val));
261 assert_eq!(opt_ref.copied(), Some(1));
263 // Double Immutable ref works
264 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
265 assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
266 assert_eq!(opt_ref_ref.copied().copied(), Some(1));
273 let opt_none: Option<&'static u32> = None;
274 let opt_ref = Some(&val);
275 let opt_ref_ref = Some(&val_ref);
278 assert_eq!(opt_none.clone(), None);
279 assert_eq!(opt_none.cloned(), None);
281 // Immutable ref works
282 assert_eq!(opt_ref.clone(), Some(&val));
283 assert_eq!(opt_ref.cloned(), Some(1));
285 // Double Immutable ref works
286 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
287 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
288 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
293 fn try_option_some() -> Option<u8> {
297 assert_eq!(try_option_some(), Some(1));
299 fn try_option_none() -> Option<u8> {
303 assert_eq!(try_option_none(), None);
307 fn test_option_as_deref() {
308 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
309 let ref_option = &Some(&42);
310 assert_eq!(ref_option.as_deref(), Some(&42));
312 let ref_option = &Some(String::from("a result"));
313 assert_eq!(ref_option.as_deref(), Some("a result"));
315 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
316 assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
318 // None: &Option<T: Deref>>::None -> None
319 let ref_option: &Option<&i32> = &None;
320 assert_eq!(ref_option.as_deref(), None);
324 fn test_option_as_deref_mut() {
325 // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
327 let ref_option = &mut Some(&mut val);
328 assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
330 let ref_option = &mut Some(String::from("a result"));
331 assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
333 let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
334 assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
336 // None: &mut Option<T: Deref>>::None -> None
337 let ref_option: &mut Option<&mut i32> = &mut None;
338 assert_eq!(ref_option.as_deref_mut(), None);
344 let old = x.replace(5);
346 assert_eq!(x, Some(5));
347 assert_eq!(old, Some(2));
350 let old = x.replace(3);
352 assert_eq!(x, Some(3));
353 assert_eq!(old, None);
358 // test that the methods of `Option` are usable in a const context
360 const OPTION: Option<usize> = Some(32);
362 const REF: Option<&usize> = OPTION.as_ref();
363 assert_eq!(REF, Some(&32));
365 const IS_SOME: bool = OPTION.is_some();
368 const IS_NONE: bool = OPTION.is_none();
371 const COPIED: Option<usize> = OPTION.as_ref().copied();
372 assert_eq!(COPIED, OPTION);
376 const fn option_const_mut() {
377 // test that the methods of `Option` that take mutable references are usable in a const context
379 let mut option: Option<usize> = Some(32);
381 let _take = option.take();
382 let _replace = option.replace(42);
386 fn test_unwrap_drop() {
391 impl<'a> std::ops::Drop for Dtor<'a> {
393 self.x.set(self.x.get() - 1);
397 fn unwrap<T>(o: Option<T>) -> T {
404 let x = &Cell::new(1);
407 let b = Some(Dtor { x });
411 assert_eq!(x.get(), 0);
416 let thing = "{{ f }}";
417 let f = thing.find("{{");
428 let z: Option<usize> = None;
430 assert_eq!(x.zip(y), Some((10, "foo")));
431 assert_eq!(x.zip(z), None);
432 assert_eq!(z.zip(x), None);
437 let x = Some((10, "foo"));
438 let y = None::<(bool, i32)>;
440 assert_eq!(x.unzip(), (Some(10), Some("foo")));
441 assert_eq!(y.unzip(), (None, None));
445 fn zip_unzip_roundtrip() {
450 assert_eq!(z, Some((10, "foo")));
453 assert_eq!(a, (x, y));