特征 `for<'r> core::ops::FnMut<(&'r &(K, T),)>` 没有为类型 `P` 实现
the trait `for<'r> core::ops::FnMut<(&'r &(K, T),)>` is not implemented for the type `P`
我想要一个(键,类型)的排序向量,它只按键排序。
我想出了下面的代码:
struct SortedVec<K, T> {
data: Vec<(K, T)>
}
impl<K: Ord, T> SortedVec<K, T> {
fn new() -> SortedVec<K, T> {
SortedVec { data: Vec::new() }
}
fn add(&mut self, k: K, t: T) {
self.data.push((k, t));
self.data.sort_by(|a, b| a.0.cmp(&b.0));
}
fn find<P>(&self, predicate: P) -> Option<&(K, T)> where P: FnMut(&(K, T)) -> bool {
self.data.iter().find(predicate)
}
}
但是编译时出现以下错误:
anon>:16:30: 16:45 error: the trait `for<'r> core::ops::FnMut<(&'r &(K, T),)>` is not implemented for the type `P` [E0277]
anon>:16 self.data.iter().find(predicate)
^~~~~~~~~~~~~~~
<anon>:16:30: 16:45 error: the trait `for<'r> core::ops::FnOnce<(&'r &(K, T),)>` is not implemented for the type `P` [E0277]
<anon>:16 self.data.iter().find(predicate)
^~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
我找不到类型 'P' 的任何错误。
我该如何解决?
好吧,编译器已经为您提供了修复提示!您需要将 "where" 子句从
更改为
fn find<P>(&self, predicate: P) -> Option<&(K, T)>
where P: FnMut(&(K, T)) -> bool
至
fn find<P>(&self, predicate: P) -> Option<&(K, T)>
where for<'r> P: FnMut(&'r &(K, T)) -> bool
让我们比较 P
上的界限和需要编译器的界限:
// P
FnMut( &(K, T)) -> bool
// required
for<'r> FnMut(&'r &(K, T)) -> bool
如果您更改 where
子句以匹配编译器要求的签名,它将起作用 (see here)。
我相信额外的引用(和生命周期)是通过使用 iter
引入的,因此与迭代器的生命周期相关联,但不要相信我的话。
不过,我要指出的是,Vec
有一个 binary_search_by
,它必然比线性 find
:
更有效
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
where F: FnMut(&T) -> Ordering
您可能希望改用它,因为您不厌其烦地对其进行排序。
我想要一个(键,类型)的排序向量,它只按键排序。
我想出了下面的代码:
struct SortedVec<K, T> {
data: Vec<(K, T)>
}
impl<K: Ord, T> SortedVec<K, T> {
fn new() -> SortedVec<K, T> {
SortedVec { data: Vec::new() }
}
fn add(&mut self, k: K, t: T) {
self.data.push((k, t));
self.data.sort_by(|a, b| a.0.cmp(&b.0));
}
fn find<P>(&self, predicate: P) -> Option<&(K, T)> where P: FnMut(&(K, T)) -> bool {
self.data.iter().find(predicate)
}
}
但是编译时出现以下错误:
anon>:16:30: 16:45 error: the trait `for<'r> core::ops::FnMut<(&'r &(K, T),)>` is not implemented for the type `P` [E0277]
anon>:16 self.data.iter().find(predicate)
^~~~~~~~~~~~~~~
<anon>:16:30: 16:45 error: the trait `for<'r> core::ops::FnOnce<(&'r &(K, T),)>` is not implemented for the type `P` [E0277]
<anon>:16 self.data.iter().find(predicate)
^~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
我找不到类型 'P' 的任何错误。
我该如何解决?
好吧,编译器已经为您提供了修复提示!您需要将 "where" 子句从
更改为fn find<P>(&self, predicate: P) -> Option<&(K, T)>
where P: FnMut(&(K, T)) -> bool
至
fn find<P>(&self, predicate: P) -> Option<&(K, T)>
where for<'r> P: FnMut(&'r &(K, T)) -> bool
让我们比较 P
上的界限和需要编译器的界限:
// P
FnMut( &(K, T)) -> bool
// required
for<'r> FnMut(&'r &(K, T)) -> bool
如果您更改 where
子句以匹配编译器要求的签名,它将起作用 (see here)。
我相信额外的引用(和生命周期)是通过使用 iter
引入的,因此与迭代器的生命周期相关联,但不要相信我的话。
不过,我要指出的是,Vec
有一个 binary_search_by
,它必然比线性 find
:
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
where F: FnMut(&T) -> Ordering
您可能希望改用它,因为您不厌其烦地对其进行排序。