为什么我需要“封面” t in`impress ofertrait< localtype> gt;对于T` [E0210]
今天,我遇到了一条非常奇怪的错误消息,我很难理解。考虑使用此简单地图 - 输入的结构:
struct Entry<K, V> {
key: K,
value: V
}
现在,我想在
entry&lt; k,v&gt;
之间实现所有std :: cmp
特征代码> k 。让我们暂时关注partialeq
。这两个实现效果很好:
impl<K: PartialEq, V> PartialEq for Entry<K, V> { /* ... */ }
impl<K: PartialEq, V> PartialEq<K> for Entry<K, V> { /* ... */ }
但是最后一个实现给了我很难的时光(游乐场)
impl<K: PartialEq, V> PartialEq<Entry<K, V>> for K {
fn eq(&self, other: &Entry<K, V>) -> bool {
self.eq(&other.key)
}
}
据我所知, 错误消息声称我将使用非本地类型作为外国性状的第一个参数。但是,条目
在同一文件中本地定义的IST。
错误[ e0210 ] k 必须在第一个本地类型之前出现其他类型(
entry&lt; k,v&gt;
)- &gt; src/lib.rs:6:6 | 6 | impl&lt; k:partialeq,v&gt; partialeq&lt;进入&lt; k,v&gt;&gt;叉 { | ^类型参数`k`必须由另一种类型覆盖,当它出现在第一种本地类型之前(``entry&lt; k,v&gt;`) |
注意:只有在实现它的至少一种类型是本地的,并且在第一个本地类型之前没有发现的类型参数
才能实现外国性状。 注意:在这种情况下,''''是指以下顺序:strans&lt; ..&gt;外国&lt; t1,...,tn&gt;对于t0
,其中t0
是第一个,tn
是最后一个
一个人可以解释为什么我会收到此错误消息,错误,尤其是未发现的意思,为什么不允许此实现?
Today I ran into a pretty strange error message that I'm having a hard time understanding. Consider this easy map-entry-like struct:
struct Entry<K, V> {
key: K,
value: V
}
Now, I want to implement all the std::cmp
traits between Entry<K, V>
with itself and with just K
. Let's focus on PartialEq
for now. These two implementations work just fine:
impl<K: PartialEq, V> PartialEq for Entry<K, V> { /* ... */ }
impl<K: PartialEq, V> PartialEq<K> for Entry<K, V> { /* ... */ }
But the last one gives me a hard time (Playground)
impl<K: PartialEq, V> PartialEq<Entry<K, V>> for K {
fn eq(&self, other: &Entry<K, V>) -> bool {
self.eq(&other.key)
}
}
The error message, as far as I can understand it, claims I'd be using a non-local type as the first parameter of the foreign trait. However, Entry
ist defined locally in the same file.
error[E0210]: type parameter
K
must be covered by another type when it appears before the first local type (Entry<K, V>
)--> src/lib.rs:6:6 | 6 | impl<K: PartialEq, V> PartialEq<Entry<K, V>> for K { | ^ type parameter `K` must be covered by another type when it appears before the first local type (`Entry<K, V>`) |
note: implementing a foreign trait is only possible if at least one of the types for which is it implemented is local, and no uncovered type parameters appear before that first local type
note: in this case, 'before' refers to the following order:impl<..> ForeignTrait<T1, ..., Tn> for T0
, whereT0
is the first andTn
is the last
Can someone explain why I am getting this error message, what the error and especially uncovered means and why this implementation is disallowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息非常差,但是阅读
现在,回顾有问题的实现,它被声明为
p1 = k,p2 = v
和t0 = k,t1 = entry&lt&lt; k,v&gt; < /code>,
ti
是t1
,因此触发了错误消息。 覆盖与将其放入一个元素元组一样容易,因此该语法完全可以:通常,覆盖可以通过将其放置在元组中(
(k,)
)或将其用作类型参数(wory stype&lt; k&gt;
),但是 not 使用参考(&amp ; k
)或一个框(box&lt; k&gt;
)。然后,仅在实施 rfc 2451 ,建议这是错误消息的一部分:
我希望Rustc能够采用该错误消息,而不是吐出目前所做的神秘信息。
The error message is pretty poor but reading the full description of E0210 very carefully gives at least an explanation as to what's going on:
Now, looking back at the implementation in question, it is declared as
So gives a mapping of
P1=K, P2=V
andT0=K, T1=Entry<K, V>
,Ti
isT1
and therefore the error message is triggered. Covering is as easy as putting it into a one-element tuple, so this syntax is totally fine:Generally, covering a type can be done by placing it into a tuple (
(K,)
) or using it as a type parameter (SomeType<K>
), but not using a reference (&K
) or a box (Box<K>
).An understandable and reasonable explanation for this is then only given in the implementing RFC 2451, which recommended this to be part of the error message:
I wish rustc would've adopted that error message rather than spitting out the cryptic one it currently does.
我认为您想要相反的方式:
至于为什么您的方向不起作用:想象一下某人定义了一个可以将可以与 的 进行比较的
struct
:现在,会有矛盾的
impl
<代码> k 和您的条目
。I think you want the other way around:
As for why your direction does not work: Imagine someone defines a
struct
that can be compared to anything:Now, there would be conflicting
impl
s forK
and yourEntry
.