You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking Issue for Option::is_some_and and Result::is_{ok,err}_and #93050
Lots of functions on Option and Result are already 'covered' by match or if 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).
let deadline_passed = optional_deadline.is_some_with(|d| d < now);let deadline_passed = matches!(optional_deadline,Some(d)if d < now);let deadline_passed = ifletSome(d) = optional_deadline && d < now {true}else{false};
Reacted by Moises Marquez, Riccardo Sacchetto, B.Sc., Frank Sachsenheim, AlephAlpha, Lukas, WMG-VincentW, Dan D'Avella, Jacobus Geluk, José Luis Cruz, Daniel Pruessner and 24 more
I assume deadline_passedwould then flow into another if in 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 filter closure? But then one could flatten() first and then filter instead.
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.
Naming. Alternatives:
Or maybe just is? Assuming we want to spend our small budget of meaningful two-letter words on this.
But I like is_some_and since it is consistent with the way the if is written.
is or 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_and flows better. I would consider the shorter some_and as well.
I assume deadline_passedwould then flow into another if in which case one could do an if-let-chain (once that's stable) directly rather than having a separate variable for that.
This example about a deadline: Option<Instant> comes directly from code I worked on recently. Checking some condition on a Some is a pattern that occurs reguarly there. In some cases it's just used in an if directly, but in many cases the value is stored in some struct first, or used by multiple ifs, or passed to a function, serialized and sent over a socket, etc. In some cases it's only used in a single if, but part of a much more complicated condition. Various parts of that condition are first assigned to named bools to keep things clear. (Just like why people use matches!() rather than a match or if let in some situations. Or x.is_some() rather than if let Some(_) = x. Or x.then() instead of if x {}. And so on.)
Or maybe just is?
I've added is and has to the alternatives.
Reacted by the8472, Jonathan de Jong, Moritz Mahringer, dlroweht and Ricardo Fernández Serrata
There are 117 cases of .map_or(false, in the compiler/ directory in the rust repository. Most of them are probably good examples of use cases for these methods.
Reacted by Ricardo Fernández Serrata
Reacted by Jonathan de Jong, vacuus, Tobias Kahlert, Olive, Lukas, Hrólfur and Ricardo Fernández Serrata
After thinking about it a bit more, I like this name. It very clearly states that it first checks is_some, and then also checks a condition afterwards.
Reacted by Jonathan de Jong, Mick Dekkers, Jonas Platte, Milo Toor, Moritz Mahringer, jam1garner, William Harrison, Matteo Martellini, filip-hejsek, Slanterns and 7 more
changed the title [-]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[/+]on Mar 29, 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_and for the name, very much a fan
Reacted by Matteo Martellini, linewelder-old, filip-hejsek, Slanterns, Mick Dekkers, FaCsaba, James0124, Osb26, Lukas, Edward Taylor and 1 more
I totally forgot about all that.. Well I guess I will live with it. The draft PR to show that it made is_some_and calls "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 extra as_ref annoying where it is needed..
Well, I don't know if these functions are really that essential. These checks can be made in several different ways.
let data = Some("foo");if data == Some("foo"){//...}if data.is_some_and(|inner| inner == "foo"){//...}ifletSome(inner) = data && inner == "foo"{//...}ifmatches!(data,Some(inner)if inner == "foo"){//...}
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.
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