RosettaCodeData/Task/Accumulator-factory/Rust/accumulator-factory.rust
2015-11-18 06:14:39 +00:00

20 lines
384 B
Text

// rustc -V
// rustc 1.2.0-nightly (0cc99f9cc 2015-05-17) (built 2015-05-18)
use std::ops::Add;
fn foo<Num>(n: Num) -> Box<FnMut(Num) -> Num>
where Num: Add<Output=Num> + Copy + 'static {
let mut acc = n;
Box::new(move |i: Num| {
acc = acc + i;
acc
})
}
fn main() {
let mut x = foo(1.);
x(5.);
foo(3.);
println!("{}", x(2.3));
}