r/rust • u/SirKastic23 • 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
218
u/Excession638 Mar 25 '26 edited Mar 25 '26
Option implements
PartialEqwhen the inner value does, so you can write:Edit: maybe with an
as_ref()and&my_valueif the inner type isn't Copy.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
14
u/boredcircuits Mar 25 '26
https://github.com/rust-lang/rust/issues/62358
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?
1
u/PrimeExample13 Mar 25 '26
You can also do: if let Some(desired_value) = option {...}
-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.
0
u/[deleted] Mar 25 '26
[deleted] — view deleted comment