预期特征 core::ops::FnMut,找到类型参数

expected trait core::ops::FnMut, found type parameter

我不明白为什么下面的代码不能编译。 rust 似乎不是 'expanding' 类型参数,因为它看起来很适合我。

代码(生锈围栏:http://is.gd/gC82I4

use std::sync::{Arc, Mutex};

struct Data{
    func: Option<Box<FnMut(String) + Send>>
}

fn newData<F>(func: Option<Box<F>>) -> Data
where F: FnMut(String) + Send{
    Data{
        func: func
    }
}

fn main(){
    let _ = newData(Some(Box::new(|msg|{})));
}

错误

<anon>:10:15: 10:19 error: mismatched types:
 expected `core::option::Option<Box<core::ops::FnMut(collections::string::String) + Send>>`,
    found `core::option::Option<Box<F>>`
(expected trait core::ops::FnMut,
    found type parameter) [E0308]
<anon>:10         func: func
                        ^~~~
error: aborting due to previous error
playpen: application terminated with error code 101

您需要至少部分地拼出从 Box<F>Box<FnMut> 的转换来帮助 rust。

因为 Box<Trait> 意味着 Box<Trait + 'static>,您还需要添加绑定 F: 'static

struct Data {
    func: Option<Box<FnMut(String) + Send>>
}

fn new_data<F>(func: Option<Box<F>>) -> Data where
    F: FnMut(String) + Send + 'static
{
    Data {
        func: func.map(|x| x as Box<_>)
    }
}

fn main() {
    let _ = new_data(Some(Box::new(|msg|{ })));
}

这里需要注意的是,Box<F>Box<FnMut ...>不是同一种类型,但大多数情况下前者会自动转换为后者。在此处的选项中,我们只需要通过编写显式转换来帮助转换。

虽然 user139873 的回答是绝对正确的,但我想补充一点,按值将闭包传递给函数并将其装箱在函数中更为惯用:

struct Data {
    func: Option<Box<FnMut(String) + Send>>
}

fn new_data<F>(func: Option<F>) -> Data where
        F: FnMut(String) + Send + 'static {
    Data {
        func: func.map(|f| Box::new(f) as Box<_>)
    }
}

fn main() {
    let _ = new_data(Some(|msg| {}));
}

这样你对调用者的限制就更少了,他们的代码也变得更简单了。