]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/rcvr-borrowed-to-slice.rs
8597bf39e0ef5346d71555b8a7fa28344996d811
[rust.git] / src / test / run-pass / rcvr-borrowed-to-slice.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 trait sum {
12     fn sum_(self) -> int;
13 }
14
15 // Note: impl on a slice
16 impl<'a> sum for &'a [int] {
17     fn sum_(self) -> int {
18         self.iter().fold(0, |a, &b| a + b)
19     }
20 }
21
22 fn call_sum(x: &[int]) -> int { x.sum_() }
23
24 pub fn main() {
25     let x = vec!(1, 2, 3);
26     let y = call_sum(x);
27     println!("y=={}", y);
28     assert_eq!(y, 6);
29
30     let x = vec!(1, 2, 3);
31     let y = x.sum_();
32     println!("y=={}", y);
33     assert_eq!(y, 6);
34
35     let x = vec!(1, 2, 3);
36     let y = x.sum_();
37     println!("y=={}", y);
38     assert_eq!(y, 6);
39 }