函数逻辑大概像这样:
const sum = (f, g) => x => f(x) + g(x);
目前我能按文档( https://doc.rust-lang.org/book/closures.html )做成下面这样,但是总是编译不过。
fn sum<F, G>(f: &'static F, g: &'static G) -> Box<Fn(i32) -> i32>
where F: Fn(i32) -> i32,
G: Fn(i32) -> i32
{
Box::new(move |x| f(x) + g(x))
}
let f1 = |x: i32| x * x;
let f2 = |x: i32| x * 2 + 1;
let a = sum(&f1, &f2);
println!("{}", a(1i32));
println!("{}", a(2i32));
println!("{}", a(3i32));