]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
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 | ||
970d7e83 | 11 | |
223e47cc | 12 | trait sum { |
c34b1796 | 13 | fn sum_(self) -> isize; |
223e47cc LB |
14 | } |
15 | ||
16 | // Note: impl on a slice | |
c34b1796 AL |
17 | impl<'a> sum for &'a [isize] { |
18 | fn sum_(self) -> isize { | |
1a4d82fc | 19 | self.iter().fold(0, |a, &b| a + b) |
223e47cc LB |
20 | } |
21 | } | |
22 | ||
c34b1796 | 23 | fn call_sum(x: &[isize]) -> isize { x.sum_() } |
223e47cc LB |
24 | |
25 | pub fn main() { | |
c30ab7b3 | 26 | let x = vec![1, 2, 3]; |
85aaf69f | 27 | let y = call_sum(&x); |
1a4d82fc | 28 | println!("y=={}", y); |
970d7e83 | 29 | assert_eq!(y, 6); |
223e47cc | 30 | |
c30ab7b3 | 31 | let x = vec![1, 2, 3]; |
85aaf69f | 32 | let y = x.sum_(); |
1a4d82fc | 33 | println!("y=={}", y); |
970d7e83 | 34 | assert_eq!(y, 6); |
223e47cc | 35 | |
c30ab7b3 | 36 | let x = vec![1, 2, 3]; |
85aaf69f | 37 | let y = x.sum_(); |
1a4d82fc | 38 | println!("y=={}", y); |
970d7e83 | 39 | assert_eq!(y, 6); |
223e47cc | 40 | } |