-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
B-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 RFCE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.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
Today I got myself in a situation in which I had two Option<T>
values, and I wanted to return the only one that was Some
. If both of them were Some
, then I'd rather return None
. I think it would be perfect to have a xor
method, very similar to the or
method:
impl<T> Option<T> {
//...
fn xor(self, optb: Option<T>) -> Option<T> {
match (&self, &optb) {
(&Some(_), &None) => self,
(&None, &Some(_)) => optb,
_ => None,
}
}
}
I think that would solve my problem nicely, and could be useful for a lot of people as well.
Object905, jswrenn, xeniagda, Centril, jeremyBanks and 11 more
Metadata
Metadata
Assignees
Labels
B-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 RFCE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.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.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
[-]Add `xor` method to `Option<T>`[/-][+]Add xor method to Option<T>[/+]clarfonthey commentedon May 7, 2018
There should also be an
xor_else
method, similar toor_else
.ollie27 commentedon May 7, 2018
There would be no point in a
xor_else
because you always have to evaluate the second argument.clarfonthey commentedon May 8, 2018
…good point. Never mind, then.
Rollup merge of rust-lang#50553 - clarcharr:option_xor, r=sfackler
andre-vm commentedon May 18, 2018
Implemented on #50553.
clarfonthey commentedon May 18, 2018
@Milack27 You actually need to keep this issue open as a tracking issue. It hasn't been stabilised yet, so, it can only be used with
#![feature(option_xor)]
at the moment.If you could rename this issue to "Tracking issue for Option::xor" it would also help make that clear.
[-]Add xor method to Option<T>[/-][+]Tracking issue for Option::xor[/+]19 remaining items
lambda-fairy commentedon Mar 1, 2019
Does this method exist in other languages? I've never seen such a method in OCaml or Haskell, both languages which make extensive use of option types and have been around much longer than Rust has. That should make us doubt whether this is useful enough to belong in the standard library.
andre-vm commentedon Mar 1, 2019
@lfairy Your point is reasonable. I'm not familiar with either OCaml or Haskell, but I'd like to point out some possible arguments in favor of Option::xor that could be taken into account when comparing Rust to those languages:
Rust already has a stable Option::or method, which works pretty much the same way. When users find it in the documentation, they may expect an Option::xor as well. If OCaml and Haskell also have an equivalent method to Option::or, but not a xor counterpart, it would be interesting to know why.
Option::xor is a zero-cost abstraction, which is a valued concept in Rust. It's a tool that allows programmers to do more with less code. But if you don't need to use it, you have no penalties. The footprint of Option::xor in the standard library is also very small. Add Option::xor method #50553 adds only 36 lines, most of which are documentation comments.
Users cannot implement this method in their own crates, as the Option type doesn't belong to them. The alternatives I can think of are a bit cumbersome, or at least not as convenient:
fn option_xor<T>(opta: Option<T>, optb: Option<T>) -> Option<T>
Centril commentedon Mar 1, 2019
Haskell's standard library has the more general operation
<|>
:and
Maybe
is an instance ofAlternative
.boomshroom commentedon Mar 1, 2019
Looking at the implementation, it seems that Haskell's implementation is an inclusive or rather than exclusive and takes the first argument if it's present regardless of the second.
rfcbot commentedon Mar 5, 2019
The final comment period, with a disposition to merge, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
The RFC will be merged soon.
Centril commentedon Mar 5, 2019
@Amanieu & @withoutboats: Since you have expressed doubts, can you confirm that you are OK with moving forward with this?
Rollup merge of rust-lang#60376 - lzutao:stabilize-option_xor, r=Simo…
Rollup merge of rust-lang#60376 - lzutao:stabilize-option_xor, r=Simo…
aaditmshah commentedon May 15, 2023
@andre-vm I just wanted to point out that
Option::xor
is not associative.However, boolean xor is associative.
Thus, it seems to me that
Option::xor
is not an exclusive or operation (over a functor) in the strictest sense. Nevertheless, it still seems to be a useful operation. Hence, I would suggest adding a remark to the documentation, elucidating thatOption::xor
is not associative.aaditmshah commentedon May 15, 2023
I would also like to point out a simpler operation than
Option::xor
, which can be used to implementOption::xor
:The
Option::xor
operation can be implemented using a combination ofOption::and_not
andOption::or
.Conversely, we can implement
Option::and_not
using a combination ofOption::xor
andOption::and
.It would be nice to have
Option::and_not
in the core library. However, even if we don't have it, it would be nice to add the previous example to the documentation ofOption::xor
.