-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the convenience method Option::is_some_and
#75298
Add the convenience method Option::is_some_and
#75298
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Isn't that essentially doing what if let chains (#53667) aim to handle? |
I usually do this with |
How about adding the de Morgan dual |
I'm inclined against adding this method. There are already many combinators on When I need to write this, I also use the (Not that it changes my opinion about adding it, but if we were to add this I would definitely expect it to have the signature |
Going by the reviewer's last comment, there is no motivation to adding this. so i'm closing this pull request. Thanks |
Adds:
I recently got the idea for this method. It solves a "pain point" (albeit a small one) I encountered many times over the years: checking if an
Option
isSome(x)
and if a condition holds forx
. Currently possible solutions:Verbose:
Concise, but the
unwrap
is not nice :/Somewhat concise, but not easy to read. At least I have to actively think about it for a few seconds before I know what exactly it does.
Concise and probably the best solution currently. However, uses a macro and the "reading flow" is not optimal (due to the two
if
s).With the new method, it would look like this:
I think this is a bit better than the
matches!
version. However, I understand there are a bunch of reasons not to add this method:Option
already has lots of helper methods and expanding the API surface has some disadvantages. If the advantage over thematches!
solution is too small, addingis_some_and
might not be worth it.true
in theNone
case. If that use case is equally as common (it is not in my experience), there should be a method for that, too. Something likeis_none_or
. But adding two methods is even worse than one, so it might not be worth it.and
in the name might be confusing because of its use inOption::and
andOption::and_then
.So I can totally understand if we decide against this addition. Just wanted to hear people's opinion on this :)
I tried searching for previous attempts to add such a method, but didn't find anything. If I missed something, please let me know.