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);
305 fn try_option_ok() -> Result<u8, NoneError> {
309 assert_eq!(try_option_ok(), Ok(1));
311 fn try_option_err() -> Result<u8, NoneError> {
315 assert_eq!(try_option_err(), Err(NoneError));
319 fn test_option_as_deref() {
320 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
321 let ref_option = &Some(&42);
322 assert_eq!(ref_option.as_deref(), Some(&42));
324 let ref_option = &Some(String::from("a result"));
325 assert_eq!(ref_option.as_deref(), Some("a result"));
327 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
328 assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
330 // None: &Option<T: Deref>>::None -> None
331 let ref_option: &Option<&i32> = &None;
332 assert_eq!(ref_option.as_deref(), None);
336 fn test_option_as_deref_mut() {
337 // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
339 let ref_option = &mut Some(&mut val);
340 assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
342 let ref_option = &mut Some(String::from("a result"));
343 assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
345 let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
346 assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
348 // None: &mut Option<T: Deref>>::None -> None
349 let ref_option: &mut Option<&mut i32> = &mut None;
350 assert_eq!(ref_option.as_deref_mut(), None);
356 let old = x.replace(5);
358 assert_eq!(x, Some(5));
359 assert_eq!(old, Some(2));
362 let old = x.replace(3);
364 assert_eq!(x, Some(3));
365 assert_eq!(old, None);
370 // test that the methods of `Option` are usable in a const context
372 const OPTION: Option<usize> = Some(32);
374 const REF: Option<&usize> = OPTION.as_ref();
375 assert_eq!(REF, Some(&32));
377 const IS_SOME: bool = OPTION.is_some();
380 const IS_NONE: bool = OPTION.is_none();
385 fn test_unwrap_drop() {
390 impl<'a> std::ops::Drop for Dtor<'a> {
392 self.x.set(self.x.get() - 1);
396 fn unwrap<T>(o: Option<T>) -> T {
403 let x = &Cell::new(1);
406 let b = Some(Dtor { x });
410 assert_eq!(x.get(), 0);
414 pub fn option_ext() {
415 let thing = "{{ f }}";
416 let f = thing.find("{{");