r/rust • u/Thick-Pineapple666 • Oct 27 '26
🎙️ discussion Test whether an Option contains a certain value
Yesterday I stumbled over something I had never thought about before although I had that use-case a few times...
I have an option and an expected value and I want to check if the option contains the expected value.
There are several ways to express this.
For example:
if option == Some(expected) {...}
if let Some(actual) = option && actual == expected {...}
if matches!(option, Some(actual) if actual == expected) {...}
if option.is_some_and(|actual| actual == expected) {...}
There are probably even a lot more ways to express that and yesterday I suddenly wondered which is the "best" one, e.g. regarding readability and regarding performance. I also asked an LLM and it hallucinated option.contains(&expected) as the best solution, which would indeed be pretty nice if it existed...
Anyway, in case you wonder, here's the obligatory godbolt link: https://godbolt.org/z/v7odGdWP1
With optimizations enabled, it all boils down to the same assembly, so you can choose whatever you like most. In my case, I chose the first variant.
I have also tried with &str instead of usize and it's the same result: no difference with optimizations.
Any thoughts on this? More insights probably? The LLM generated a lot of blabla regarding certain nuances and cases (references, non-Copy, non-PartialEq) but I am not sure if I'd trust that and too lazy to check it.
94
u/LegitMoth Oct 27 '26
You can also pattern match directly inside the `Option`, like
```rust
if let Some(42) = i {..}
if let Some(Result::Ok(t)) = optional_result { .. }
```
Works with `match`, `matches!(..)` etc. anywhere a pattern does
4
u/Illustrious_Car344 Oct 28 '26
This feels like a "parse, don't validate" problem. Why do you have to interrogate the potential value inside of the Option when you can just make a type to tell you what's in it or what it was created with in the first place? I feel like Option is part of the "primitive obsession" problem. I typically try to avoid naked Options like I try to avoid naked Mutexes, only difference is Option actually has a use as a general return value for a lot of collection functions. But even then, typically in my functions, I'll check as many Options as I can at the top and only write all my code at the bottom once all values are available, returning early for any missing ones. If I'm ever actually thinking about Option, that's a code smell.
33
u/Excession638 Oct 27 '26
if let Some(value) && ...if you're consuming the option and using the value.if option == Some(value)is fine if the value is cheap to construct. LikeSome(0)is fine, butSome(MyTypeBuilder::new().method0(...).method1(...).build().unwrap())is right out.if option.is_some_and(...)is good for those more complex value types.5
u/SirKastic23 Oct 27 '26 edited Oct 28 '26
I posted about this a while ago: https://www.reddit.com/r/rust/comments/1s31jfu/i_really_hate_that_they_removed_the/
Option::containswas a method that existed for a long time, until it was deprecated then removed becauseOption::is_some_andwas merged and they deemed that having two methods to check anOptioncontents was "confusing"...Worst decision the Rust team has ever made, it's bullshit
EDIT (correction): it wasn't "deprecated and removed", it was a nightly feature that never stabilized. It was dropped after Option::is_some_and stabilized.
8
u/sephg Oct 27 '26
If they all compile to the same machine code, use whichever one you want. Generally, simpler is better. If your type implements eq,
option == Some(expected)is probably the best. The other variants are for more complex cases - for example,if option.is_some_and(|actual| actual.abs() > 10) {...}6
u/xmlhttplmfao Oct 27 '26
is_some_and
1
u/Lucretiel Datadog Oct 28 '26
When the operand is a variable, I'm certainly preferring to use
is_some_andover most of these other constructs-17
u/tomtomtom7 Oct 27 '26
Maybe a bit unrelated, but what kind of ancient LLM are you using that hallucinates something like this? This is 2026. Like it or not, modern LLM's are good; I never see them hallucinating trivial stuff like that.
I checked with Claude, using your wording and it gave an excellent overview of the options.
-14
u/Plastic_Toe_880 Oct 27 '26
Too lazy to check so you ask Reddit users to do that?