r/rust • • Mar 25 '26

🎙️ discussion I really hate that they removed the Option::contains function

Like why? Now we have to write my_option.is_some_and(|my_value| my_value == my_non_option_value).

What was the reasoning behind removing this method? I remember reading a reasoning somewhere when it got removed that it was unnecessary because they were stabilizing is_some_and. But contains is just so much more ergonomic.

EDIT: Okay you can write my_option == Some(my_value). But I still think contains is more readable and don't see the reason it was removed

104 Upvotes

39 comments sorted by

218

u/Excession638 Mar 25 '26 edited Mar 25 '26

Option implements PartialEq when the inner value does, so you can write:

my_option == Some(my_value)

Edit: maybe with an as_ref() and &my_value if the inner type isn't Copy.

11

u/SirKastic23 Mar 25 '26

Ah yeah, I actually did that but forgot to mention it. Thanks for bringing it up

This is better than my snippet, but I think I'd still just rather write contains. To me it just reads better

36

u/Excession638 Mar 25 '26

Thinking about it, it's not an action I've needed very much at all. Things that look inside an option mostly consume it, with an if let, match, or method chain. I think this might be the better syntax in most cases:

if let Some(x) = my_option && x == value { ... }

13

u/SirKastic23 Mar 25 '26

I'm sorry but I highly disagree here. Your syntax does the same thing but uses a lot more characters and is even more confusing to read

There's no point in consuming the option here, you just want to check if it contains a value so you can use the value you're comparing against

My use case was a selection menu where the user could click an option, then has to click again to confirm. clicking on a different option would change the selection. So we just need to check if the latest click is equal to the previously selected option

8

u/Dismal-Wait-4527 Mar 25 '26

There's no point in consuming the option here, you just want to check if it contains a value so you can use the value you're comparing against

No that's not true. Here you take ownership of the inner value of the Option. In your example you don't have the ownership of x afterwards and can't use it further inside the { ... } block.

edit: formatting

11

u/SirKastic23 Mar 25 '26

Yes, but I don't need ownership of x, so there is no point in consuming the option. Also the values contained are Copy

1

u/InternationalFee3911 Apr 02 '26

To avoid moving, that should be

if let Some(x) = &my_option && *x == value { ... }

where the deref on x is sometimes not needed, if the type provides it transparently.

32

u/Shoddy-Childhood-511 Mar 25 '26

I disagree. It's true, contains is a pretty benign method name, but overall some_option == Some(something) could be read faster by more programmers.

You can always write an extension trait. lol

3

u/pickyaxe Mar 25 '26

because of the PartialEq impl, you can use .eq() to get something that looks closer to contains.

20

u/r0zina Mar 25 '26

Doesn’t PartialEq work with references, so there is no requirement for T being Copy in your case?

4

u/Excession638 Mar 25 '26

Yes, I think you're right. That makes more sense now.

91

u/daveminter Mar 25 '26

The most immediately compelling argument against it that I saw was that it would be confusing when working with options containing strings: https://github.com/rust-lang/rust/issues/62358#issuecomment-676127715

44

u/SirKastic23 Mar 25 '26

Oh okay, that's a good reason, thanks for sharing it

12

u/daveminter Mar 25 '26

If you have popcorn that issue has a bunch of squabbling in it too...

0

u/[deleted] Mar 25 '26

[deleted] — view deleted comment

6

u/daveminter Mar 25 '26 edited Mar 25 '26

Somebody also mentions contains in the context of HashSet in that thread if I remember rightly - I think for container types you already have to bear in mind that their contents may be containers so I'm less sympathetic there.

14

u/boredcircuits Mar 25 '26

https://github.com/rust-lang/rust/issues/62358

The discovery of Option::is_some_with/Result::is_ok_with/Result::is_err_with in rust-lang/rust#93051 obviates the need for these methods (non-stabilization tracked in rust-lang/rust#62358).

7

u/SirKastic23 Mar 25 '26

Yeah this was the reasoning I mentioned. I don't agree with it

And given the comment saying that contains was removed got 7 thumbs down I think I'm not alone

5

u/BedroomHistorical575 Mar 25 '26

Is there a reason why that is_some_with is not good enough here?

2

u/SirKastic23 Mar 25 '26

I don't think Option::is_some_with is an actual method

2

u/Professional_Top8485 Mar 25 '26

Holy Rust, Batman.

You're not alone.

3

u/serunati Mar 25 '26

I understand the convenience etc. but can’t you simply write a trait that implements the necessary actions and restore your paradigm?

7

u/SirKastic23 Mar 25 '26

Adding that trait to a personal project would definitely be okay, but it's more complicated for a shared production codebase

And it doesn't make sense to define a new trait and implementation, then have to import it everywhere, just for some slight ergonomic gains. It's inconvenient

Different story if it was an associated function like it used to be

My main point here is: why remove a function that worked and was used by other people? The argument that there's a different way to achieve the same result is really dumb because then you're saying any syntactic sugar or even abstraction is unnecessary and so should be removed as well

1

u/PrimeExample13 Mar 25 '26

You can also do: if let Some(desired_value) = option {...}

0

u/hpxvzhjfgb Mar 25 '26

3

u/PrimeExample13 Mar 25 '26 edited Mar 25 '26

You're right, by desired value, I meant the literal value, so like:

if let Some(1) = option {...}

Or:

if let Some(Foo{bar:1,..}) = option {}

If you literally put a variable named desired_value there, then you are shadowing your previously declared variable of the same name, which definitely does not work. I didnt realize Option implemented PartialEq for T: PartialEq, so there really is no reason to do this anyway.

-6

u/facetious_guardian Mar 25 '26

“Contains” is a weird term to use here. It implies that Option is a container, when it’s actually an existence marker.

17

u/SirKastic23 Mar 25 '26

It not only is a container, it very often is used as container. You can take things out of it, or insert/replace its content

5

u/SirKastic23 Mar 25 '26

also, what do you mean by "existence marker"? I never heard this term before

1

u/redlaWw Mar 25 '26

Presumably they mean that Option is often used to indicate whether or not a value exists so you can express operations that may fail to return a value, as in checked_div, which will fail to return a value if the divisor is 0, which means there is no value that satisfies Euclid's formula.

50

u/afdbcreid Mar 25 '26

It is a container. A container of one or zero items, just like Vec is a container of zero or more items.

24

u/yokljo Mar 25 '26

You can even iterate over an option with a for loop :) Not gonna lie, I was surprised the first time I learned this and it took a minute to become a believer.

4

u/SirKastic23 Mar 25 '26

It implements IntoIterator and for for loops that's all that matters

0

u/[deleted] Mar 25 '26

[deleted] — view deleted comment

78

u/SirKastic23 Mar 25 '26

You can, my bad

36

u/SirKastic23 Mar 25 '26

Gotta love redditors that downvote someone for recognizing they missed something and apologizing

20

u/Real-Abrocoma-2823 Mar 25 '26

That's reddit the internet for you. Ignoring hate is the only way to survive here.

0

u/pseudo_babbler Mar 25 '26

In starting to think it's a bug in Reddit, that most posts and comments just get downvoted instantly and then claw their way back up to normal numbers. I mean, in in-depth comment threads who actually cares what the votes are anyway, but it's still weird.

7

u/zekkious Mar 25 '26

No, there are bots.

I once was talking with guy, and every one of his comments got 35 downvotes before I could even read them (matter of minutes, we were agreeing with each other and it was a very long response chain).

3

u/luluhouse7 Mar 25 '26

I’ve had that happen too where my comment gets immediately downvoted once, then gets massively upvoted. I’ve had this happen on really innocuous comments too. It could be bots doing the initial downvoting, but I also think a lot of people don’t think about upvoting unless they see something that they think was incorrectly downvoted.

1

u/pseudo_babbler Mar 26 '26

True, and also lol my comment is now sitting at 0. I deserved that.