2012-12-11 01:32:48 +00:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-01-31 20:35:36 +00:00
|
|
|
use std::mem::swap;
|
2013-05-06 04:42:54 +00:00
|
|
|
|
2014-12-31 04:32:49 +00:00
|
|
|
#[derive(Show)]
|
2014-05-06 01:56:44 +00:00
|
|
|
struct Ints {sum: Box<int>, values: Vec<int> }
|
2012-07-24 23:23:23 +00:00
|
|
|
|
2013-01-26 06:46:32 +00:00
|
|
|
fn add_int(x: &mut Ints, v: int) {
|
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-01-02 22:32:54 +00:00
|
|
|
fn iter_ints<F>(x: &Ints, mut f: F) -> bool where F: FnMut(&int) -> bool {
|
2012-07-24 23:23:23 +00:00
|
|
|
let l = x.values.len();
|
2014-10-15 06:05:01 +00:00
|
|
|
range(0u, 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() {
|
2014-05-06 01:56:44 +00:00
|
|
|
let mut ints = box Ints {sum: box 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| {
|
2014-12-20 08:09:35 +00:00
|
|
|
println!("int = {:?}", *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
|
|
|
}
|