2018-09-04 05:05:53 -05:00
|
|
|
// run-pass
|
2015-01-07 19:25:56 -06:00
|
|
|
|
2014-01-31 14:35:36 -06:00
|
|
|
use std::mem::swap;
|
2013-05-05 23:42:54 -05:00
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Debug)]
|
2015-03-25 19:06:52 -05:00
|
|
|
struct Ints {sum: Box<isize>, values: Vec<isize> }
|
2012-07-24 18:23:23 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn add_int(x: &mut Ints, v: isize) {
|
2012-07-24 18:23:23 -05:00
|
|
|
*x.sum += v;
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut values = Vec::new();
|
2014-01-31 14:35:36 -06:00
|
|
|
swap(&mut values, &mut x.values);
|
2012-09-26 19:33:34 -05:00
|
|
|
values.push(v);
|
2014-01-31 14:35:36 -06:00
|
|
|
swap(&mut values, &mut x.values);
|
2012-07-24 18:23:23 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn iter_ints<F>(x: &Ints, mut f: F) -> bool where F: FnMut(&isize) -> bool {
|
2012-07-24 18:23:23 -05:00
|
|
|
let l = x.values.len();
|
2015-03-03 02:42:26 -06:00
|
|
|
(0..l).all(|i| f(&x.values[i]))
|
2012-07-24 18:23:23 -05:00
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut ints: Box<_> = Box::new(Ints {sum: Box::new(0), values: Vec::new()});
|
2014-06-25 01:11:57 -05:00
|
|
|
add_int(&mut *ints, 22);
|
|
|
|
add_int(&mut *ints, 44);
|
2012-07-24 18:23:23 -05:00
|
|
|
|
2014-07-07 18:35:15 -05:00
|
|
|
iter_ints(&*ints, |i| {
|
2015-03-25 19:06:52 -05:00
|
|
|
println!("isize = {:?}", *i);
|
2013-08-02 01:17:20 -05:00
|
|
|
true
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2012-07-24 18:23:23 -05:00
|
|
|
|
2014-12-20 02:09:35 -06:00
|
|
|
println!("ints={:?}", ints);
|
2012-07-24 18:23:23 -05:00
|
|
|
}
|