Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need some help regarding an error on HashMap and PartialOrd

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

more code + error

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading