如何在新线程中调用递归闭包?

时间:2018-10-18 08:18:06

标签: rust

tool是有用的函数的抓包,用于函数式编程,包括making recursive closure,例如:

extern crate tool;
use tool::prelude::*;

fn main() {
    let fib = fix(move |f, x| {
        if x == 0 || x == 1 {
            x
        } else {
            // `f` is `fib`
            f(x - 1) + f(x - 2)
        }
    });

    println!("{}", fib(10)); // print 55
}

我想要实现的东西在这里:playground

这里是概述:我正在定义一个function A,它以一个function B作为参数,同样,function B也以function C作为参数。我需要在新线程中调用function C,在这种情况下,对内部函数使用&dyn关键字会给我一个错误:

fn a(b: impl Fn(&dyn Fn() -> ()) -> ()) -> () {
 // ...
} 
error[E0277]: `dyn std::ops::Fn() ->()` cannot be shared between threads safely

我尝试如下语法,但这给了我另一个错误:

fn a(b: impl Fn(impl Fn() -> ()) -> ()) -> () {
 // ...
}  
error[E0666]: nested `impl Trait` is not allowed
 --> src/main.rs:2:21
  |
2 |     fn a(b: impl Fn(impl Fn() -> ()) -> ()) -> () {
  |             --------^^^^^^^^^^^^^^^-------
  |             |       |
  |             |       nested `impl Trait` here
  |             outer `impl Trait`

0 个答案:

没有答案