Skip to content

Python-like chained comparison operators #77823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mayabyte opened this issue Oct 11, 2020 · 2 comments
Closed

Python-like chained comparison operators #77823

mayabyte opened this issue Oct 11, 2020 · 2 comments

Comments

@mayabyte
Copy link

Python allows chaining comparison operators as a helpful shorthand:

if a < b < c:  # equivalent to (a < b and b < c)
    ...

However Rust explicitly disallows this:

fn main() {
    5 < 10 < 15
}

// error: comparison operators cannot be chained
// --> chain.rs:2:5
//  |
// 2 |   5 < 10 < 15
//  |     ^    ^
//  |
// help: split the comparison into two
//  |
// 2 |   5 < 10 && 10 < 15
//  |          ^^^^^
//
// error: aborting due to previous error

rust-lang/rfcs#558 seems to be the source of this explicit disallow, with the primary motivation being confusing behavior: a < b < c used to be interpreted as (a < b) < c, resulting in a type error. The RFC also claims this would allow implentation of Python-like chained comparison operators in the future. Is there any motivation not to add these Python-like chained comparison operators into Rust? Given that the syntax for it has been a compiler error since pre-1.0, it doesn't seem like it could be a breaking change. Thoughts?

@jonas-schievink
Copy link
Contributor

Hi! Additions or changes to the language need to go through our RFC process. Before an RFC is written, the feature can also be discussed in our internals forum to find other people interested in it.

Closing in favor of an RFC.

@mayabyte
Copy link
Author

The internals forum does sound like a better place for this. Thanks for the tip!
(As an aside, I didn't find anything suggesting the internals forum while I was looking into opening this issue. Perhaps there's room for better documentation pointing there?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants