Equality comparison of enums with literals

In my code I often want to do something like if some_result == Ok(3). However I can't because PartialEq isn't implemented for Error. But clearly in this case it doesn't need to be - at no point will it need to check Error for equality.

Is anyone working on fixing this? I guess it would depend on enum variant types which as far as I can see is not planned?

You can write something like some_result.is_ok_and(|val| *val == 3)

1 Like

Alternatively, you can use matches!(some_result, Ok(3)) or even with conditional clause matches!(some_result, Ok(x) if x % 2 == 0).

2 Likes