下面的代码由于类型错误无法编译:
这个例子代码来自一个更大的程序,其中
error[E0283]: type annotations required: cannot resolve _: std::cmp::Eq
。为了编译此代码需要什么类型注释?这个例子代码来自一个更大的程序,其中
MyHashGenerator::hash_node()
用于哈希AST中的节点(类型T
与AST节点内保存的值相关,但不需要重现此类型错误时的AST定义)。use std::hash::Hash;
pub trait HashGenerator<T: Clone + Eq + Hash + ToString> {
fn hash(&self, msg: &str) -> u64; // Hash a string.
fn hash_node(&self) -> u64; // Hash an AST node.
}
struct MyHashGenerator {}
impl<T: Clone + Eq + Hash + ToString> HashGenerator<T> for MyHashGenerator {
fn hash(&self, msg: &str) -> u64 {
0
}
fn hash_node(&self) -> u64 {
// error[E0283]: type annotations required: cannot resolve `_: std::cmp::Eq`
self.hash("")
}
}
这个问题类似于这里的问题,但没有得到解答。
MyHashGenerator
实现Eq
并不能解决错误。 - snim2