我不明白为什么下面的代码不能编译。似乎 rust 只是没有“扩展”类型参数,因为它看起来与我匹配。
代码(防锈围栏: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|{})));
}
Run Code Online (Sandbox Code Playgroud)
错误
<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
Run Code Online (Sandbox Code Playgroud)
您需要至少部分拼出从Box<F>
to的演员表来帮助生锈Box<FnMut>
。
因为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|{ })));
}
Run Code Online (Sandbox Code Playgroud)
这里要注意的是,Box<F>
和Box<FnMut ...>
不是同一种类型,但在大多数情况下,前者会自动转换为后者。在此处的 Option 中,我们只需要通过编写显式强制转换来帮助转换。
归档时间: |
|
查看次数: |
1721 次 |
最近记录: |