Skip to content

Method to invert/toggle a boolean #820

Description

@Quelfth

Proposal

Problem statement

Every so often when I'm writing code involving bool, I end up needing to do the equivalent of

my_bool = !my_bool;

However this can be verbose if the name of the boolean is long, or if it is acessed through some long method/field chain. In this case I could introduce another variable but in my opinion this obfuscates the meaning. Also any time a variable name is used twice, there is some risk of changing one of them but not the other causing a bug.

The alternative I usually reach for in this case is

my_bool ^= true;

However I think most will agree that this little trick is fairly confusing if you have not seen it before, and a bit even if you have. I always have to remind myself "is it ^= true or ^= false?".

Motivating examples or use cases

I would expect that many people have needed to toggle a boolean before. Many ui buttons cause this kind of behavior. I can provide some real world examples if we're not convinced.

Solution sketch

impl bool {
    pub fn toggle(&mut self) {
        *self = !*self;
    }
}

Alternatives

The only alternative I can think of to a method is some kind of dedicated operator such as my_bool!!; but, I don't think this use case is common enough to justify that.

Links and related work

It seems that this has been discussed in this thread:
https://internals.rust-lang.org/t/idea-invert-or-toggle-function-for-boolean-type/8860

A function with this exact name and behavior exists in Swift:
https://developer.apple.com/documentation/swift/bool/toggle()

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)api-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions