我有以下内容:
fn apply_bin(&mut self, op: Fn(i32,i32)) -> Result<i32, String> {
}
但我得到了错误:
<anon>:75:29: 75:31 error: the trait `core::marker::Sized` is not implemented for the type `core::ops::Fn(i32, i32) + 'static` [E0277]
<anon>:75 fn apply_bin(&mut self, op: Fn(i32,i32)) -> Result<i32, String> {
^~
为什么会这样,我该如何解决?
您可以使用通用方法,因为它Fn
是一个特征:
fn apply_bin<F>(&mut self, op: F) -> Result<i32, String>
where F: Fn(i32, i32)
{
}
(这是静态调度)
您还可以使用动态调度:
fn apply_bin(&mut self, op: &Fn(i32, i32)) -> Result<i32, String>
{
}
还有的更多信息本书。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句