在使用盒装闭包时,我遇到了以下问题:
type Test = Rc<dyn Fn() -> i64>;
fn test_bad() -> Test {
Test::new(|| 42)
}
fn test_good() -> Test {
Rc::new(|| 42)
}
在第一种情况下,我使用类型别名来引用方法,而在第二种情况下new
我直接使用。Rc
在第一种情况下,编译器抱怨:
| Test::new(|| 42)
| ^^^ function or associated item not found in `Rc<(dyn Fn() -> i64 + 'static)>`
|
= note: the method `new` exists but the following trait bounds were not satisfied:
`dyn Fn() -> i64: Sized`
但第二种情况效果很好。有人可以解释一下区别吗?有什么方法可以new
通过类型别名引用还是我需要自己包装它?