Skip to content

The enum type in Guide is not working #17967

@chenyukang

Description

@chenyukang
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");
    }
}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions