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);
168 let mut it = x.iter();
170 assert_eq!(it.size_hint(), (1, Some(1)));
171 assert_eq!(it.next(), Some(&val));
172 assert_eq!(it.size_hint(), (0, Some(0)));
173 assert!(it.next().is_none());
175 let mut it = (&x).into_iter();
176 assert_eq!(it.next(), Some(&val));
184 let mut x = Some(val);
186 let mut it = x.iter_mut();
188 assert_eq!(it.size_hint(), (1, Some(1)));
192 assert_eq!(*interior, val);
195 None => assert!(false),
198 assert_eq!(it.size_hint(), (0, Some(0)));
199 assert!(it.next().is_none());
201 assert_eq!(x, Some(new_val));
203 let mut y = Some(val);
204 let mut it = (&mut y).into_iter();
205 assert_eq!(it.next(), Some(&mut val));
210 let small = Some(1.0f64);
211 let big = Some(5.0f64);
212 let nan = Some(0.0f64 / 0.0);
213 assert!(!(nan < big));
214 assert!(!(nan > big));
215 assert!(small < big);
222 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
223 assert!(v == Some(vec![]));
225 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
226 assert!(v == Some(vec![0, 1, 2]));
228 let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
231 // test that it does not take more elements than it needs
232 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
233 [box || Some(()), box || None, box || panic!()];
235 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
244 let opt_none: Option<&'static u32> = None;
245 let opt_ref = Some(&val);
246 let opt_ref_ref = Some(&val_ref);
249 assert_eq!(opt_none.clone(), None);
250 assert_eq!(opt_none.copied(), None);
252 // Immutable ref works
253 assert_eq!(opt_ref.clone(), Some(&val));
254 assert_eq!(opt_ref.copied(), Some(1));
256 // Double Immutable ref works
257 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
258 assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
259 assert_eq!(opt_ref_ref.copied().copied(), Some(1));
266 let opt_none: Option<&'static u32> = None;
267 let opt_ref = Some(&val);
268 let opt_ref_ref = Some(&val_ref);
271 assert_eq!(opt_none.clone(), None);
272 assert_eq!(opt_none.cloned(), None);
274 // Immutable ref works
275 assert_eq!(opt_ref.clone(), Some(&val));
276 assert_eq!(opt_ref.cloned(), Some(1));
278 // Double Immutable ref works
279 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
280 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
281 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
286 fn try_option_some() -> Option<u8> {
290 assert_eq!(try_option_some(), Some(1));
292 fn try_option_none() -> Option<u8> {
296 assert_eq!(try_option_none(), None);
298 fn try_option_ok() -> Result<u8, NoneError> {
302 assert_eq!(try_option_ok(), Ok(1));
304 fn try_option_err() -> Result<u8, NoneError> {
308 assert_eq!(try_option_err(), Err(NoneError));
312 fn test_option_as_deref() {
313 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
314 let ref_option = &Some(&42);
315 assert_eq!(ref_option.as_deref(), Some(&42));
317 let ref_option = &Some(String::from("a result"));
318 assert_eq!(ref_option.as_deref(), Some("a result"));
320 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
321 assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
323 // None: &Option<T: Deref>>::None -> None
324 let ref_option: &Option<&i32> = &None;
325 assert_eq!(ref_option.as_deref(), None);
329 fn test_option_as_deref_mut() {
330 // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
332 let ref_option = &mut Some(&mut val);
333 assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
335 let ref_option = &mut Some(String::from("a result"));
336 assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
338 let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
339 assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
341 // None: &mut Option<T: Deref>>::None -> None
342 let ref_option: &mut Option<&mut i32> = &mut None;
343 assert_eq!(ref_option.as_deref_mut(), None);
349 let old = x.replace(5);
351 assert_eq!(x, Some(5));
352 assert_eq!(old, Some(2));
355 let old = x.replace(3);
357 assert_eq!(x, Some(3));
358 assert_eq!(old, None);
363 // test that the methods of `Option` are usable in a const context
365 const OPTION: Option<usize> = Some(32);
367 const REF: Option<&usize> = OPTION.as_ref();
368 assert_eq!(REF, Some(&32));
370 const IS_SOME: bool = OPTION.is_some();
373 const IS_NONE: bool = OPTION.is_none();
378 fn test_unwrap_drop() {
383 impl<'a> std::ops::Drop for Dtor<'a> {
385 self.x.set(self.x.get() - 1);
389 fn unwrap<T>(o: Option<T>) -> T {
396 let x = &Cell::new(1);
399 let b = Some(Dtor { x });
403 assert_eq!(x.get(), 0);
407 pub fn option_ext() {
408 let thing = "{{ f }}";
409 let f = thing.find("{{");