]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/pure-sum.rs
Merge branch 'master' into cfg_tmp_dir
[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
14 fn sums_to(v: Vec<int> , sum: int) -> bool {
15     let mut i = 0u;
16     let mut sum0 = 0;
17     while i < v.len() {
18         sum0 += v[i];
19         i += 1u;
20     }
21     return sum0 == sum;
22 }
23
24 fn sums_to_using_uniq(v: Vec<int> , sum: int) -> bool {
25     let mut i = 0u;
26     let mut sum0 = box 0;
27     while i < v.len() {
28         *sum0 += v[i];
29         i += 1u;
30     }
31     return *sum0 == sum;
32 }
33
34 fn sums_to_using_rec(v: Vec<int> , sum: int) -> bool {
35     let mut i = 0u;
36     let mut sum0 = F {f: 0};
37     while i < v.len() {
38         sum0.f += v[i];
39         i += 1u;
40     }
41     return sum0.f == sum;
42 }
43
44 struct F<T> { f: T }
45
46 fn sums_to_using_uniq_rec(v: Vec<int> , sum: int) -> bool {
47     let mut i = 0u;
48     let mut sum0 = F {f: box 0};
49     while i < v.len() {
50         *sum0.f += v[i];
51         i += 1u;
52     }
53     return *sum0.f == sum;
54 }
55
56 pub fn main() {
57 }