2012-12-10 19:32:48 -06: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.
|
|
|
|
|
2015-01-07 19:25:56 -06:00
|
|
|
#![allow(unknown_features)]
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
|
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)]
|
2014-05-05 20:56:44 -05:00
|
|
|
struct Ints {sum: Box<int>, values: Vec<int> }
|
2012-07-24 18:23:23 -05:00
|
|
|
|
2013-01-26 00:46:32 -06:00
|
|
|
fn add_int(x: &mut Ints, v: int) {
|
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-01-02 16:32:54 -06:00
|
|
|
fn iter_ints<F>(x: &Ints, mut f: F) -> bool where F: FnMut(&int) -> bool {
|
2012-07-24 18:23:23 -05:00
|
|
|
let l = x.values.len();
|
2015-02-18 04:42:01 -06:00
|
|
|
(0_usize..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() {
|
2015-02-17 14:41:32 -06:00
|
|
|
let mut ints: Box<_> = box Ints {sum: box 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| {
|
2014-12-20 02:09:35 -06:00
|
|
|
println!("int = {:?}", *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
|
|
|
}
|