4
我试图返回一个以&Any
作为参数的方法。以下代码返回编译器错误。非标量演员:`方块<FnMut<&Any>>`
trait Selector {
fn id(&self) -> i64;
fn selector(&self) -> Box<FnMut(&Any, &mut Any)>;
}
struct TypedSelector<TSource, TTarget> {
id: i64,
select: Box<FnMut(&TSource, &mut TTarget)>,
}
impl<TSource, TTarget> Selector for TypedSelector<TSource, TTarget>
where TSource: 'static,
TTarget: 'static
{
fn id(&self) -> i64 {
self.id
}
fn selector(&self) -> Box<FnMut(&Any, &mut Any)> {
self.select as Box<FnMut(&Any, &mut Any)>
}
}
编译器错误是:
error: non-scalar cast: `Box<for<'r, 'r> std::ops::FnMut(&'r TSource, &'r mut TTarget) + 'static>` as `Box<for<'r, 'r> std::ops::FnMut(&'r std::any::Any + 'static, &'r mut std::any::Any + 'static)>`
--> src\main.rs:190:9
|
190 | self.select as Box<FnMut(&Any, &mut Any)>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我缺少某种类型的注解?
我知道什么是'unwrap'是诱人的,但它可能是最好的教新人使用'expect'代替。它确实能够诊断出哪里出了问题。 –
@MatthieuM。好点,更新。 –