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