传递函数并返回Rust中的盒装闭包

时间:2017-04-27 14:54:44

标签: function rust closures traits lifetime

我正努力做到这一点:

type Real = f64;

pub fn outer<F>(f1: &F, f2: &F) -> Box<Fn(&Real) -> Real>
    where F: Fn(&Real) -> Real
{
    Box::new(move |x| f1(x) + f2(x))
}

编译器一直在抱怨:

error[E0477]: the type `[closure@src/main.rs:6:14: 6:36 f1:&F, f2:&F]` does not fulfill the required lifetime
 --> src/main.rs:6:5
  |
6 |     Box::new(move |x| f1(x) + f2(x))
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: type must outlive the static lifetime

如何让它发挥作用?

0 个答案:

没有答案