Compare tuple values of an enum variant

I want to compare the second and third strings of a tuple variant of two enum instances.
How do I do that?

enum ExCrateEnum {
    TupleOne(String, String, String),
    TupleTwo(String),
}

fn main() {
    let enum_one: ExCrateEnum = ExCrateEnum::TupleOne("string_one".to_string(), "string_two".to_string(), "string_three".to_string());
    let enum_two: ExCrateEnum = ExCrateEnum::TupleOne("string_four".to_string(), "string_two".to_string(), "string_three".to_string());
    
    assert_eq!(enum_one("",enum_one_second_string,enum_one_third_string), enum_two("",enum_two_second_string,enum_two_third_string));
}
enum ExCrateEnum {
    TupleOne(String, String, String),
    TupleTwo(String),
}

fn main() {
    let enum_one: ExCrateEnum = ExCrateEnum::TupleOne("string_one".to_string(), "string_two".to_string(), "string_three".to_string());
    let enum_two: ExCrateEnum = ExCrateEnum::TupleOne("string_four".to_string(), "string_two".to_string(), "string_three".to_string());
    
    let enums_partially_equal: bool = match (&enum_one, &enum_two) {
        (MyEnum::TupleOne(_, a1, a2), MyEnum::TupleOne(_, b1, b2)) => a1 == b1 && a2 == b2
    };

    assert_eq!(enums_partially_equal, true);
}
    

You could also

assert!(matches!((&enum_one,&enum_two),
    (ExCrateEnum::TupleOne(_, a1, a2), ExCrateEnum::TupleOne(_, b1, b2))
    if a1 == b1 && a2 == b2
));
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.