rust/src/test/run-pass/rcvr-borrowed-to-slice.rs
2012-08-01 19:16:06 -07:00

32 lines
543 B
Rust

trait sum {
fn sum() -> int;
}
// Note: impl on a slice
impl foo of sum for &[int] {
fn sum() -> int {
let mut sum = 0;
for vec::each(self) |e| { sum += e; }
return sum;
}
}
fn call_sum(x: &[int]) -> int { x.sum() }
fn main() {
let x = ~[1, 2, 3];
let y = call_sum(x);
debug!{"y==%d", y};
assert y == 6;
let x = ~[mut 1, 2, 3];
let y = x.sum();
debug!{"y==%d", y};
assert y == 6;
let x = ~[1, 2, 3];
let y = x.sum();
debug!{"y==%d", y};
assert y == 6;
}