3 use core::clone::Clone;
9 let addr_x: *const isize = mem::transmute(&*x);
12 let addr_y: *const isize = mem::transmute(&*y);
13 assert_eq!(addr_x, addr_y);
19 let x = "test".to_string();
20 let addr_x = x.as_ptr();
23 let addr_y = y.as_ptr();
24 assert_eq!(addr_x, addr_y);
28 fn test_get_resource() {
30 use core::cell::RefCell;
33 i: Rc<RefCell<isize>>,
40 *ii.borrow_mut() = i + 1;
44 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();
71 #[test] #[should_panic]
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");
134 fn test_unwrap_panic1() {
135 let x: Option<isize> = None;
142 fn test_unwrap_panic2() {
143 let x: Option<String> = None;
148 fn test_unwrap_or() {
149 let x: Option<isize> = Some(1);
150 assert_eq!(x.unwrap_or(2), 1);
152 let x: Option<isize> = None;
153 assert_eq!(x.unwrap_or(2), 2);
157 fn test_unwrap_or_else() {
158 let x: Option<isize> = Some(1);
159 assert_eq!(x.unwrap_or_else(|| 2), 1);
161 let x: Option<isize> = None;
162 assert_eq!(x.unwrap_or_else(|| 2), 2);
170 let mut it = x.iter();
172 assert_eq!(it.size_hint(), (1, Some(1)));
173 assert_eq!(it.next(), Some(&val));
174 assert_eq!(it.size_hint(), (0, Some(0)));
175 assert!(it.next().is_none());
177 let mut it = (&x).into_iter();
178 assert_eq!(it.next(), Some(&val));
186 let mut x = Some(val);
188 let mut it = x.iter_mut();
190 assert_eq!(it.size_hint(), (1, Some(1)));
194 assert_eq!(*interior, val);
197 None => assert!(false),
200 assert_eq!(it.size_hint(), (0, Some(0)));
201 assert!(it.next().is_none());
203 assert_eq!(x, Some(new_val));
205 let mut y = Some(val);
206 let mut it = (&mut y).into_iter();
207 assert_eq!(it.next(), Some(&mut val));
212 let small = Some(1.0f64);
213 let big = Some(5.0f64);
214 let nan = Some(0.0f64/0.0);
215 assert!(!(nan < big));
216 assert!(!(nan > big));
217 assert!(small < big);
224 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
225 assert!(v == Some(vec![]));
227 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
228 assert!(v == Some(vec![0, 1, 2]));
230 let v: Option<Vec<isize>> = (0..3).map(|x| {
231 if x > 1 { None } else { Some(x) }
235 // test that it does not take more elements than it needs
236 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
237 [box || Some(()), box || None, box || panic!()];
239 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
248 let opt_none: Option<&'static u32> = None;
249 let opt_ref = Some(&val);
250 let opt_ref_ref = Some(&val_ref);
253 assert_eq!(opt_none.clone(), None);
254 assert_eq!(opt_none.copied(), None);
256 // Immutable ref works
257 assert_eq!(opt_ref.clone(), Some(&val));
258 assert_eq!(opt_ref.copied(), Some(1));
260 // Double Immutable ref works
261 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
262 assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
263 assert_eq!(opt_ref_ref.copied().copied(), Some(1));
270 let opt_none: Option<&'static u32> = None;
271 let opt_ref = Some(&val);
272 let opt_ref_ref = Some(&val_ref);
275 assert_eq!(opt_none.clone(), None);
276 assert_eq!(opt_none.cloned(), None);
278 // Immutable ref works
279 assert_eq!(opt_ref.clone(), Some(&val));
280 assert_eq!(opt_ref.cloned(), Some(1));
282 // Double Immutable ref works
283 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
284 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
285 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
290 fn try_option_some() -> Option<u8> {
294 assert_eq!(try_option_some(), Some(1));
296 fn try_option_none() -> Option<u8> {
300 assert_eq!(try_option_none(), None);
302 fn try_option_ok() -> Result<u8, NoneError> {
306 assert_eq!(try_option_ok(), Ok(1));
308 fn try_option_err() -> Result<u8, NoneError> {
312 assert_eq!(try_option_err(), Err(NoneError));
316 fn test_option_deref() {
317 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
318 let ref_option = &Some(&42);
319 assert_eq!(ref_option.deref(), Some(&42));
321 let ref_option = &Some(String::from("a result"));
322 assert_eq!(ref_option.deref(), Some("a result"));
324 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
325 assert_eq!(ref_option.deref(), Some(&[1, 2, 3, 4, 5][..]));
327 // None: &Option<T: Deref>>::None -> None
328 let ref_option: &Option<&i32> = &None;
329 assert_eq!(ref_option.deref(), None);
335 let old = x.replace(5);
337 assert_eq!(x, Some(5));
338 assert_eq!(old, Some(2));
341 let old = x.replace(3);
343 assert_eq!(x, Some(3));
344 assert_eq!(old, None);