Pre-RFC: Option eq_some method

Currently, I can write one of:

  1. if option == Some(foo)
  2. if matches!(option, Some(v) if v == foo)

The first is shorter and more readable but the second seems more idiomatic and performant.

So, I would like to be able to write the following:

if option.eq_some(&foo)

Would this be a good addition to Option in std?

I disagree. I think the first is perfectly idiomatic, and their performance is identical. For example, these two functions compile to the same machine code.

If you want to write the first as a method instead of an operator, you can already write:

if option.eq(&Some(foo))

which is only one character longer than your proposal.

3 Likes

It's just not very obvious to me that the compiler will optimize away creating the right hand side Option.

Ah, yeah, it can be a problem if the Option contains a non-Copy type that you don’t want to clone or move. In those cases you have to do something like:

option.as_ref() == Some(&foo)

or

option.map_or(false, |x| x == foo)

Lo and behold, this is already underway. See tracking issue for contains.

2 Likes

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.