Enum variable comparison Error

enum Ordering {
    Less,
    Equal,
    Greater,
}

fn cmp(a: int, b: int) -> Ordering {
    if a < b { 
       Less 
    }
    else if a > b { 
       Greater 
    }
    else { 
        Equal 
     }
}

fn main() {
    let x = 5i;
    let y = 10i;
     let ordering = cmp(x, y);

   if ordering == Less {
        println!("less");
    } else if ordering == Greater {
        println!("greater");
    } else if ordering == Equal {
        println!("equal");
    }
}

Please edit your post to use code blocks for both code and error messages.

```
// your code here
```

do u knw how to compare the enum variables

Yes. Please fix your post.

There error message is:

<anon>:21:8: 21:24 error: binary operation `==` cannot be applied to type `Ordering`
<anon>:21     if ordering == Less {
                 ^~~~~~~~~~~~~~~~
<anon>:23:15: 23:34 error: binary operation `==` cannot be applied to type `Ordering`
<anon>:23     } else if ordering == Greater {
                        ^~~~~~~~~~~~~~~~~~~
<anon>:25:15: 25:32 error: binary operation `==` cannot be applied to type `Ordering`
<anon>:25     } else if ordering == Equal {
                        ^~~~~~~~~~~~~~~~~
error: aborting due to 3 previous errors
playpen: application terminated with error code 101

edited the post...can u please tell me how to avoid this error.

It's still not in a code block. You make a code block by surrounding the code with three backticks, like this:

```
// your code here
```

I'll answer your question now, but please use code blocks from now on:

#[derive(Eq, PartialEq)]
enum Ordering {
    Less,
    Equal,
    Greater,
}

Thanks for ur reply........
what is the use of
#[derive(Eq, PartialEq)]

It allows you to use == on the enum.

1 Like

This works for me. View playground.

1 Like

The indentation makes me sad. This is why we need to use code blocks.

#[derive(Eq, PartialEq)]
enum Ordering {
    Less,
    Equal,
    Greater,
}

fn cmp(a: i8, b: i8) -> Ordering {
    if a < b {
        Ordering::Less
    } else if a > b {
        Ordering::Greater
    } else {
        Ordering::Equal
    }
}

fn main() {
    let x = 5;
    let y = 10;
    let ordering = cmp(x, y);

    if ordering == Ordering::Less {
        println!("less");
    } else if ordering == Ordering::Greater {
        println!("greater");
    } else if ordering == Ordering::Equal {
        println!("equal");
    }
}
2 Likes

Got it thanks

derive is a keyword which lets the compiler implement basic functionality for you, so you don't have to write it yourself. In this case the basic functionality is Eq and PartialEq which allows you to compare your struct/enum/... More info here.

1 Like

Thanks for ur reply

I just encountered this thread. Your first post is very difficult to read, because you still have not edited that post to use code blocks as @alice repeatedly requested. Please go to the top post on this thread and edit it by adding code block lines before your first line and after your last line. Thank you in advance for all future readers of this thread.

@TomP @alice it looks like @Trivenu tried to edit the post, but made some mistakes while doind so. I've editted the OP to fix the code fence.

TIA from all of us.

@Trivenu, note that instead of implementing PartialEq for the Ordering enum you have the option of using the very different solution of pattern matching on it:

fn main() {
    let x = 5i;
    let y = 10i;
    let ordering = cmp(x, y);

   match ordering {
     Ordering::Less => println!("less"),
     Ordering::Greater => println!("greater"),
     Ordering::Equal => println!("equal");
    }
}