2014-07-23 14:43:29 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-10-02 21:48:07 -05:00
|
|
|
fn each<'a,T,F:FnMut(&'a T)>(x: &'a [T], mut f: F) {
|
2015-01-31 11:20:46 -06:00
|
|
|
for val in x {
|
2014-07-23 14:43:29 -05:00
|
|
|
f(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut sum = 0;
|
|
|
|
let elems = [ 1, 2, 3, 4, 5 ];
|
2015-03-25 19:06:52 -05:00
|
|
|
each(&elems, |val: &usize| sum += *val);
|
2014-07-23 14:43:29 -05:00
|
|
|
assert_eq!(sum, 15);
|
|
|
|
}
|