Skip to content

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

Closed
@cdouglass

Description

@cdouglass

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions