]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/borrowck/borrowck-mut-uniq.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / run-pass / borrowck / borrowck-mut-uniq.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(box_syntax)]
12
13 use std::mem::swap;
14
15 #[derive(Debug)]
16 struct Ints {sum: Box<isize>, values: Vec<isize> }
17
18 fn add_int(x: &mut Ints, v: isize) {
19     *x.sum += v;
20     let mut values = Vec::new();
21     swap(&mut values, &mut x.values);
22     values.push(v);
23     swap(&mut values, &mut x.values);
24 }
25
26 fn iter_ints<F>(x: &Ints, mut f: F) -> bool where F: FnMut(&isize) -> bool {
27     let l = x.values.len();
28     (0..l).all(|i| f(&x.values[i]))
29 }
30
31 pub fn main() {
32     let mut ints: Box<_> = box Ints {sum: box 0, values: Vec::new()};
33     add_int(&mut *ints, 22);
34     add_int(&mut *ints, 44);
35
36     iter_ints(&*ints, |i| {
37         println!("isize = {:?}", *i);
38         true
39     });
40
41     println!("ints={:?}", ints);
42 }