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]
72 fn test_option_too_much_dance() {
75 let _y2 = y.take().unwrap();
76 let _y3 = y.take().unwrap();
81 let x: Option<isize> = Some(1);
82 assert_eq!(x.and(Some(2)), Some(2));
83 assert_eq!(x.and(None::<isize>), None);
85 let x: Option<isize> = None;
86 assert_eq!(x.and(Some(2)), None);
87 assert_eq!(x.and(None::<isize>), None);
92 let x: Option<isize> = Some(1);
93 assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
94 assert_eq!(x.and_then(|_| None::<isize>), None);
96 let x: Option<isize> = None;
97 assert_eq!(x.and_then(|x| Some(x + 1)), None);
98 assert_eq!(x.and_then(|_| None::<isize>), None);
103 let x: Option<isize> = Some(1);
104 assert_eq!(x.or(Some(2)), Some(1));
105 assert_eq!(x.or(None), Some(1));
107 let x: Option<isize> = None;
108 assert_eq!(x.or(Some(2)), Some(2));
109 assert_eq!(x.or(None), None);
114 let x: Option<isize> = Some(1);
115 assert_eq!(x.or_else(|| Some(2)), Some(1));
116 assert_eq!(x.or_else(|| None), Some(1));
118 let x: Option<isize> = None;
119 assert_eq!(x.or_else(|| Some(2)), Some(2));
120 assert_eq!(x.or_else(|| None), None);
125 assert_eq!(Some(1).unwrap(), 1);
126 let s = Some("hello".to_string()).unwrap();
127 assert_eq!(s, "hello");
132 fn test_unwrap_panic1() {
133 let x: Option<isize> = None;
139 fn test_unwrap_panic2() {
140 let x: Option<String> = None;
145 fn test_unwrap_or() {
146 let x: Option<isize> = Some(1);
147 assert_eq!(x.unwrap_or(2), 1);
149 let x: Option<isize> = None;
150 assert_eq!(x.unwrap_or(2), 2);
154 fn test_unwrap_or_else() {
155 let x: Option<isize> = Some(1);
156 assert_eq!(x.unwrap_or_else(|| 2), 1);
158 let x: Option<isize> = None;
159 assert_eq!(x.unwrap_or_else(|| 2), 2);
167 let mut it = x.iter();
169 assert_eq!(it.size_hint(), (1, Some(1)));
170 assert_eq!(it.next(), Some(&val));
171 assert_eq!(it.size_hint(), (0, Some(0)));
172 assert!(it.next().is_none());
174 let mut it = (&x).into_iter();
175 assert_eq!(it.next(), Some(&val));
183 let mut x = Some(val);
185 let mut it = x.iter_mut();
187 assert_eq!(it.size_hint(), (1, Some(1)));
191 assert_eq!(*interior, val);
194 None => assert!(false),
197 assert_eq!(it.size_hint(), (0, Some(0)));
198 assert!(it.next().is_none());
200 assert_eq!(x, Some(new_val));
202 let mut y = Some(val);
203 let mut it = (&mut y).into_iter();
204 assert_eq!(it.next(), Some(&mut val));
209 let small = Some(1.0f64);
210 let big = Some(5.0f64);
211 let nan = Some(0.0f64/0.0);
212 assert!(!(nan < big));
213 assert!(!(nan > big));
214 assert!(small < big);
221 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
222 assert!(v == Some(vec![]));
224 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
225 assert!(v == Some(vec![0, 1, 2]));
227 let v: Option<Vec<isize>> = (0..3).map(|x| {
228 if x > 1 { None } else { Some(x) }
232 // test that it does not take more elements than it needs
233 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
234 [box || Some(()), box || None, box || panic!()];
236 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
245 let opt_none: Option<&'static u32> = None;
246 let opt_ref = Some(&val);
247 let opt_ref_ref = Some(&val_ref);
250 assert_eq!(opt_none.clone(), None);
251 assert_eq!(opt_none.copied(), None);
253 // Immutable ref works
254 assert_eq!(opt_ref.clone(), Some(&val));
255 assert_eq!(opt_ref.copied(), Some(1));
257 // Double Immutable ref works
258 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
259 assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
260 assert_eq!(opt_ref_ref.copied().copied(), Some(1));
267 let opt_none: Option<&'static u32> = None;
268 let opt_ref = Some(&val);
269 let opt_ref_ref = Some(&val_ref);
272 assert_eq!(opt_none.clone(), None);
273 assert_eq!(opt_none.cloned(), None);
275 // Immutable ref works
276 assert_eq!(opt_ref.clone(), Some(&val));
277 assert_eq!(opt_ref.cloned(), Some(1));
279 // Double Immutable ref works
280 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
281 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
282 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
287 fn try_option_some() -> Option<u8> {
291 assert_eq!(try_option_some(), Some(1));
293 fn try_option_none() -> Option<u8> {
297 assert_eq!(try_option_none(), None);
299 fn try_option_ok() -> Result<u8, NoneError> {
303 assert_eq!(try_option_ok(), Ok(1));
305 fn try_option_err() -> Result<u8, NoneError> {
309 assert_eq!(try_option_err(), Err(NoneError));
313 fn test_option_deref() {
314 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
315 let ref_option = &Some(&42);
316 assert_eq!(ref_option.deref(), Some(&42));
318 let ref_option = &Some(String::from("a result"));
319 assert_eq!(ref_option.deref(), Some("a result"));
321 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
322 assert_eq!(ref_option.deref(), Some(&[1, 2, 3, 4, 5][..]));
324 // None: &Option<T: Deref>>::None -> None
325 let ref_option: &Option<&i32> = &None;
326 assert_eq!(ref_option.deref(), None);
332 let old = x.replace(5);
334 assert_eq!(x, Some(5));
335 assert_eq!(old, Some(2));
338 let old = x.replace(3);
340 assert_eq!(x, Some(3));
341 assert_eq!(old, None);