简体   繁体   English

特性`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`

I want a sorted vector of (Key, Type), which is sorted only by Key. 我想要一个(Key,Type)的排序向量,它只按Key排序。

I came up with the code below: 我想出了下面的代码:

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)
    }
}

But this doesn't compile with the following errors: 但是这不能编译以下错误:

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

I can't find anything wrong with the type 'P'. 我找不到类型'P'的任何问题。

How can I fix it? 我该如何解决?

Let's compare the bound on P and that required the compiler: 让我们比较P上的界限和编译器所需的界限:

// P
        FnMut(    &(K, T)) -> bool

// required
for<'r> FnMut(&'r &(K, T)) -> bool

If you change the where clause to match the signature asked for by the compiler, it works ( see here ). 如果更改where子句以匹配编译器要求的签名,则它可以工作( 请参阅此处 )。

I believe the extra reference (and lifetime) are introduced by the use of iter , and thus linked to the lifetime of the iterator, but don't take my word for it. 我相信额外的引用(和生命周期)是通过使用iter引入的,因此链接到迭代器的生命周期,但是不要相信我的话。

I would point out, though, that Vec has a binary_search_by which is bound to be more efficient than a linear find : 不过,我会指出, Vec有一个binary_search_by ,它必然比线性find更有效:

fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
    where F: FnMut(&T) -> Ordering

You may wish to use that instead, since you went to the trouble of sorting it. 您可能希望使用它,因为您遇到了排序它的麻烦。

Well, the compiler already gave you a hint at the fix! 那么,编译器已经给你一个修复提示! You need to change the "where" clause from 您需要更改“where”子句

fn find<P>(&self, predicate: P) -> Option<&(K, T)> 
    where P: FnMut(&(K, T)) -> bool

to

fn find<P>(&self, predicate: P) -> Option<&(K, T)> 
   where for<'r> P: FnMut(&'r &(K, T)) -> bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 期望的trait core :: ops :: FnMut,找到类型参数 - expected trait core::ops::FnMut, found type parameter 未为`T`实现特征`std :: ops :: Fn &lt;(char,)&gt;` - The trait `std::ops::Fn<(char,)>` is not implemented for `T` Trait`core :: ops :: Index <i32> `没有实施 - Trait `core::ops::Index<i32>` is not implemented 将在FnMut上实现的特征传递到辅助函数而无需移动 - Passing trait implemented on FnMut into helper function without moving 在尝试拆分字符串时,没有为`String`实现特性`FnMut &lt;(char,)&gt;` - The trait `FnMut<(char,)>` is not implemented for `String` when trying to split a string 将连接池传递给 Hyper 处理程序时,闭包的“trait core::ops::Fn&lt;(Request, Response)&gt; 未实现” - "trait core::ops::Fn<(Request, Response)> is not implemented" for a closure when passing a connection pool to a Hyper handler 特征 std::ops::Try 没有为 impl 实现 - the trait std::ops::Try is not implemented for impl 具有泛型类型的结构向量“未实现特征core :: marker :: Sized`” - “the trait `core::marker::Sized` is not implemented” for a vector of structs with a generic type 什么是特性`core :: types :: Sized`没有实现生锈中的类型`<generic#0>`? - What is the trait `core::kinds::Sized` is not implemented for the type `<generic #0>` in rust? 类型绑定中的特征“未实现”? - Trait in type bound “not implemented”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM