I am writing an interpreter in Rust and came across this error that I don’t know how to solve. This error occurred when I was implementing a HashMap in the interpreter using the built-in HashMap in Rust.
#[derive(Debug, PartialEq, PartialOrd, Clone, Eq, Hash)]
pub enum Expression {
Identifier(String),
Literal(Literal),
Prefix(Token, Box<Expression>),
Infix(Box<Expression>, Token, Box<Expression>),
Index(Box<Expression>, Box<Expression>),
IfExpr(Box<Expression>, BlockStatement, Option<BlockStatement>),
Func(Option<Vec<String>>, BlockStatement),
FuncCall(Box<Expression>, Vec<Expression>),
}
#[derive(Debug, PartialEq, Clone, Eq, PartialOrd, Hash)]
pub enum Literal {
Integer(i64),
Bool(bool),
String(String),
Array(Vec<Expression>),
Hash(HashMap<Expression, Expression>),
}
The error says that I didn’t implement PartialOrd for HashMap<Expression, Expression>, but to my understanding, HashMap is unordered, so why would I need to implement that trait for it? And after I followed the compiler’s instruction an add PartialOrd for Expression the error still exist, and I have no idea how to fix it? Could someone give me a hand? Thanks
>Solution :
Above the enum you have #[derive(…, PartialOrd,…)]. This means that all enum variants should satisfy the trait PartialOrd. That’s why the compiler wants your hashmap to satisfy the PartialOrd. Either remove the Hashmap from the variant or remove this requirement at all.
And same problem with the trait Hash