Repository navigation
Closed
Labels
A-option-resultArea: Option and Result combinatorsArea: Option and Result combinatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.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 RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-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.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.This issue requires a nightly compiler in some way. When possible, use a F-* label instead.
Description
Tracking issue for the bool::then_some method, which abstract the following pattern:
if some_condition {
Some(t)
} else {
None
}bool:then has previously been stabilised as part of this feature gate: #79299
RFC: rust-lang/rfcs#2757
Implementation PR: #64255
Reacted by Dror Levin, Jacob Greenfield, chpio, Darren Kaste, evdokimovs, Ellen Emilia Anna Zscheile, Stargateur, Igor Aleksanov, LOU Xun, runiq and 85 more
Reacted by Alberto Leal, Eric Shimizu Karbstein, Iago-lito, cyqsimon, Andrew Hlynskyi, Tony Beta Lambda and zohnannor
Reacted by Stepan Koltsov
Reacted by Jonas Platte, Edwin Cheng, chpio, Darren Kaste, Jacob Greenfield, evdokimovs, Stargateur, Igor Aleksanov, Emanuel Rylke, runiq and 24 more
Reacted by Alberto Leal
Reacted by Alberto Leal, thekashifmalik and Cory Forsstrom
Metadata
Metadata
Assignees
Labels
A-option-resultArea: Option and Result combinatorsArea: Option and Result combinatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.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 RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-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.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.This issue requires a nightly compiler in some way. When possible, use a F-* label instead.
Activity
[-]Add methods for converting `bool` to `Option<T>[/-][+]Add methods for converting `bool` to `Option<T>`[/+][-]Add methods for converting `bool` to `Option<T>`[/-][+]Tracking issue for methods converting `bool` to `Option<T>`[/+]U007D commentedon Nov 19, 2019
In trying this out, I was puzzled why the eager/lazy argument evaluation versions are
then/then_withand notand/and_then, which seems more consistent with established patterns instd?varkor commentedon Nov 19, 2019
There's a long discussion in the RFC about the naming. The current names are essentially placeholders.
jplatte commentedon Nov 19, 2019
As a datapoint, this feature turns out pretty useful in proc macros, and I do think the current naming reads nicely: https://github.com/ruma/ruma-api/commit/cb00188e0371c544c4b8622d5a32731f2f6c54b3
varkor commentedon Nov 19, 2019
@jplatte: that would be helpful to share on the RFC!
c410-f3r commentedon Dec 21, 2019
It's been sometime since the PR implementation.
Is there anything preventing stabilization? If not, I can write a stabilization report for this feature.
varkor commentedon Dec 21, 2019
@c410-f3r: the names were only decided upon quite recently. It was decided that we would wait a few versions before deciding to stabilise: rust-lang/rfcs#2757 (comment).
U007D commentedon Jan 26, 2020
Just came back to give this a test drive in a library I am working on. If we look at common current established patterns in
std:Option:map<U, F> whereF: FnOnce(T) -> Uandand_then<U, F> whereF: FnOnce(T) -> Option<U>Result:map<U, F> whereF: FnOnce(T) -> Uandand_then<U, F> whereF: FnOnce(T) -> Option<U>And looking at the current implementation of the
bool_to_optionfeature:bool:then_somewhereF: FnOnce() -> TThe inconsistency is pretty jarring/unexpected, to my mind. Specifically,
bool'sF: FnOnce() -> Tbe calledmap, andF: FnOnce() -> Option<T>be somethenderivative (and_then,then_some, etc.)?bool_to_optionalso provide both anF: FnOnce() -> Tand anF: FnOnce() -> Option<T>implementation?This is not just hypothetical; I am working on a library with a type that has a two-step fallible constructor. The intent is to be able to write something along the lines of:
but with the current plan developers would still have to resort to writing a custom combinator or pull in an external crate like
boolinatorfor this simple case.Thoughts?
RustyYato commentedon Jan 26, 2020
You can call
bool::thenfollowed byOption::flattento get the same effect. But it does make sense to have a fallible version ofbool::then134 remaining items
jplatte commentedon Dec 28, 2020
Stabilized; I think this tracking issue can be closed 🙂
CryZe commentedon Dec 28, 2020
Only
thengot stabilized,then_someis still being tracked.m-ou-se commentedon Dec 28, 2020
It might be a good idea to move
then_someto a new tracking issue and close this one.[-]Tracking issue for methods converting `bool` to `Option<T>`[/-][+]Tracking issue for `bool::then_some`[/+]varkor commentedon Dec 28, 2020
I've updated the title and description. I don't think there's any need to create a new issue: that just causes unnecessary churn.
slerpyyy commentedon Dec 28, 2020
Does that mean we're still considering stabilizing
then_someafter all?I thought we decided that adding
then_somewas redundant, sincethencan already do everythingthen_somecan and more.varkor commentedon Dec 30, 2020
@slerpyyy: yes, see #64260 (comment). There was a desire to see how useful
then_somewas in practice as a shorthand. This pattern (taking a value vs taking a closure) is already common throughout the standard library.KodrAus commentedon Jan 13, 2021
Closing this one in favour of #80967, so that we'll get a chance to issue a fresh FCP if we decide to in the future.
matklad commentedon Nov 23, 2021
As an experience report, I've recently found roughly the following code in the wild:
It takes some mental puzzle-solving to figure out whether
is_uppercaseresults in error or not.Don't think this is actionable in any way, but it's a useful real-world specimen to ponder.
thomaseizinger commentedon Nov 23, 2021
I think that is a good example that not everything is better with combinators :)
I haven't decipered 100% it but I would write this as:
Personally, I've been using
.then()primarily with.filter_map. I think it is a fantasic fit for that.Lucretiel commentedon Nov 23, 2021
Agree, I've seen some similar code in the wild. I think that usually
.then()is only appropriate in cases where anOption(orResult, viaok_or_else) is actually in play;.then()followed immediately by*_or_elseis probably an antipattern (because you're needlessly round-tripping through anOption). I'd love to see a clippy lint to this effect.