]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/pure-sum.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / pure-sum.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 // Check that functions can modify local state.
12
13 #![allow(unknown_features)]
14 #![feature(box_syntax)]
15
16 fn sums_to(v: Vec<int> , sum: int) -> bool {
17     let mut i = 0u;
18     let mut sum0 = 0;
19     while i < v.len() {
20         sum0 += v[i];
21         i += 1u;
22     }
23     return sum0 == sum;
24 }
25
26 fn sums_to_using_uniq(v: Vec<int> , sum: int) -> bool {
27     let mut i = 0u;
28     let mut sum0 = box 0;
29     while i < v.len() {
30         *sum0 += v[i];
31         i += 1u;
32     }
33     return *sum0 == sum;
34 }
35
36 fn sums_to_using_rec(v: Vec<int> , sum: int) -> bool {
37     let mut i = 0u;
38     let mut sum0 = F {f: 0};
39     while i < v.len() {
40         sum0.f += v[i];
41         i += 1u;
42     }
43     return sum0.f == sum;
44 }
45
46 struct F<T> { f: T }
47
48 fn sums_to_using_uniq_rec(v: Vec<int> , sum: int) -> bool {
49     let mut i = 0u;
50     let mut sum0 = F {f: box 0};
51     while i < v.len() {
52         *sum0.f += v[i];
53         i += 1u;
54     }
55     return *sum0.f == sum;
56 }
57
58 pub fn main() {
59 }