Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust Guide section on enums is confusing since PartialEq, Eq traits are not derived by default #18155

Closed
cdouglass opened this issue Oct 19, 2014 · 1 comment

Comments

@cdouglass
Copy link

The guide gives example code (below) that doesn't compile for this reason, but does not mention deriving anywhere, and I found this confusing.

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");
    }
}
@steveklabnik
Copy link
Member

As the guide says, the enum is provided by Rust itself. You have re-defined and shadowed the original enum. If you don't, it works just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants