Repository navigation
Closed
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Description
Feature gate: #![feature(is_some_and)]
This is a tracking issue for Option::is_some_and, Result::is_ok_and and Result::is_err_and.
Public API
impl<T> Option<T> {
pub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool;
}
impl<T, E> Result<T, E> {
pub fn is_ok_and(self, f: impl FnOnce(T) -> bool) -> bool;
pub fn is_err_and(self, f: impl FnOnce(E) -> bool) -> bool;
}Steps / History
- Lots of discussion and bikeshedding starting here: Tracking issue for
Option::containsandResult::contains#62358 (comment) - Implementation: Add Option::is_some_with and Result::is_{ok,err}_with #93051
- Rename: Rename is_{some,ok,err}_with to is_{some,ok,err}_and. #94765
- Take
selfby value: Changeis_some_andto take by value #98354 - Final comment period (FCP)
- Stabilization PR: Stabilize is_some_and #110019
Unresolved Questions
- Naming. Alternatives:
is_some_with/is_ok_with/is_err_with- ➡️
is_some_and/is_ok_and/is_err_and contains/contains_errand_is/and_err_isishas
- Take
selfor&self?
Reacted by Jonathan de Jong, Moritz Mahringer, JmPotato, Dominic Clifton, Dixon Sean Low Yan Feng, AlephAlpha, David Young, jam1garner, Matteo Martellini, Philipp Herzog and 104 more
Reacted by Arniu Tseng, Stepan Koltsov and Kalle Wachsmuth
Reacted by soc
Reacted by Frank Steffahn and Arniu Tseng
Reacted by Dan D'Avella, Grey, Vijay Ramamurthy and Maria Solano
Reacted by Vijay Ramamurthy
Reacted by Vijay Ramamurthy and Ricardo Fernández Serrata
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Activity
the8472 commentedon Jan 18, 2022
What are the use-cases? And won't that largely be covered by if-let chains?
m-ou-se commentedon Jan 18, 2022
Lots of functions on
OptionandResultare already 'covered' bymatchorif let, but it's still very useful to have methods for the most common cases. It makes things more readable and less verbose, and also easier to write (especially with auto completion).the8472 commentedon Jan 18, 2022
I assume
deadline_passedwould then flow into anotherifin which case one could do an if-let-chain (once that's stable) directly rather than having a separate variable for that. I mean as I understand it the purpose of if-let-chain is to make such towers of conditionals more readable.Maybe it's useful inside a
filterclosure? But then one couldflatten()first and thenfilterinstead.I think a code example with a bit more context that would benefit from this and wouldn't be replaced by if-let-chain in the future would be helpful.
Or maybe just
is? Assuming we want to spend our small budget of meaningful two-letter words on this.But I like
is_some_andsince it is consistent with the way the if is written.camsteffen commentedon Jan 18, 2022
isor any name ending in "is" will not flow well with function names in the argument:opt.and_is(char::is_lowercase),dog.and_is(Dog::has_a_bone), etc.is_some_andflows better. I would consider the shortersome_andas well.m-ou-se commentedon Jan 18, 2022
This example about a
deadline: Option<Instant>comes directly from code I worked on recently. Checking some condition on aSomeis a pattern that occurs reguarly there. In some cases it's just used in anifdirectly, but in many cases the value is stored in some struct first, or used by multipleifs, or passed to a function, serialized and sent over a socket, etc. In some cases it's only used in a singleif, but part of a much more complicated condition. Various parts of that condition are first assigned to namedbools to keep things clear. (Just like why people usematches!()rather than amatchorif letin some situations. Orx.is_some()rather thanif let Some(_) = x. Orx.then()instead ofif x {}. And so on.)I've added
isandhasto the alternatives.m-ou-se commentedon Jan 18, 2022
There are 117 cases of
.map_or(false,in thecompiler/directory in the rust repository. Most of them are probably good examples of use cases for these methods.jakoschiko commentedon Jan 19, 2022
The name
is_some_andwould allow us to add its counterpartis_none_orin the future.m-ou-se commentedon Jan 22, 2022
After thinking about it a bit more, I like this name. It very clearly states that it first checks
is_some,andthen also checks a condition afterwards.camsteffen commentedon Mar 7, 2022
FWIW this was attempted in #75298.
Rollup merge of rust-lang#94765 - m-ou-se:is-some-and, r=Dylan-DPC
Use Rust nightly in CI
[-]Tracking Issue for Option::is_some_with and Result::is_{ok,err}_with[/-][+]Tracking Issue for Option::is_some_and and Result::is_{ok,err}_and[/+]jam1garner commentedon May 4, 2022
As someone who finds map_or both overly verbose for this (I need this version far more than the general case of map_or) and generally unpleasant (due to argument order not matching the name, although I 100% understand that decision even if I don't love it) I would be thrilled for this.
I think the worry about overlap with if/let is valid but for filtering and other combinators this composes really well (having played with it some now)
Also +1 to
is_some_andfor the name, very much a fan94 remaining items
jplatte commentedon Apr 5, 2023
Is this just waiting for somebody to create a stabilization PR? I can probably do that if so.
Amanieu commentedon Apr 5, 2023
Yes. Feel free to go ahead.
asquared31415 commentedon Apr 6, 2023
The description of this API in this tracking issue and in #110019 say that the closure takes
&Tbut the implementation has the closure takingT.Amanieu commentedon Apr 6, 2023
We had a second FCP to change the closure to take
T, but it seems that we forgot to update the issue description. I've just updated it.jplatte commentedon Apr 7, 2023
I totally forgot about all that.. Well I guess I will live with it. The draft PR to show that it made
is_some_andcalls "nicer" unfortunately didn't show any places where taking ownership of of the inner value was necessary, but maybe there are a few and it would probably be pretty annoying for some people for the combinator to not be applicable therethr same way I find the extraas_refannoying where it is needed..Auto merge of rust-lang#110019 - jplatte:jplatte/stabilize-is-some-an…
Replace unstable library feature
wrenger commentedon May 19, 2023
Well, I don't know if these functions are really that essential. These checks can be made in several different ways.
The first might only be desirable in some cases, but the third one with
let-chains#53667 is a little bit more legible, in my opinion.tnblumer commentedon Jun 5, 2023
In which version should we expect this feature? I thought it was going to be in 1.70.0, but apparently not.
RalfJung commentedon Jun 6, 2023
It is in 1.70.0 (see https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html)
fix: avoid is_some_with
fix: avoid is_some_with
fix: avoid is_some_with