函数A
接受一个函数B
作为参数,而函数B
又接受函数C
作为参数。我尝试了以下类似的语法,但是出现了错误:
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`
由于某些原因,我无法使用&dyn
关键字:
fn a(b: impl Fn(&dyn Fn() -> ()) -> ()) -> () {
// ...
}
有没有其他的方法来做这件事?
我不知道为什么嵌套的impl Trait
会导致错误。
&dyn
关键字,我需要在新线程中调用函数C,否则会导致错误:cannot be shared between threads safely
。 - 周汉成