# Python-like chained comparison operators

**URL:** <https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191>\
**Category:** language design\
**Created:** [October 11, 2020, 5:19pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191 "2020-10-11T17:19:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayabyte](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mayabyte/32/7244_2.png) [@mayabyte](https://internals.rust-lang.org/u/mayabyte)\
**Post date:** [October 11, 2020, 5:19pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/1 "2020-10-11T17:19:58Z")

</div>

Python allows chaining comparison operators as a helpful shorthand:

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

```

However Rust explicitly disallows this:

```rust
fn main() { 
    5 < 10 < 15 
} 
// error: comparison operators cannot be chained 
// --> chain.rs:2:5 
// | 
// | 5 < 10 < 15 
// | ^ ^ 
// | 
// help: split the comparison into two 
// | 
// | 5 < 10 && 10 < 15 
// | ^^^^^ 
// 
// error: aborting due to previous error

```

[rust-lang/rfcs#558](https://github.com/rust-lang/rfcs/pull/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?

---

<div class="post-metadata">

**Author:** ![tesuji](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tesuji/32/12790_2.png) [@tesuji](https://internals.rust-lang.org/u/tesuji)\
**Post date:** [October 11, 2020, 5:33pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/2 "2020-10-11T17:33:56Z")

</div>

This should be valid Rust, but it is compilation error now:

```rust
5 < 3 < true

```

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [October 11, 2020, 5:34pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/3 "2020-10-11T17:34:13Z")

</div>

There's no _fundamental_ reason not to allow it, and this change was intentionally forward-compatible with the possibility. It would require a lang project proposal (MCP), and a spec.

The spec should cover things such as limiting the types of operators that can chain; for instance, `a < b < c` should work, as should `a >= b > c`, but `==` and `!=` should not chain, and chains with operators in opposite directions like `a < b >= c` should not work.

The spec would also need to document the desugaring, which should only evaluate each piece once and preserve left-to-right evaluation order; for instance, `a() < b() < c()` would desugar to `{ let temp1 = a(); let temp2 = b(); let temp3 = c(); temp1 < temp2 && temp2 < temp3 }`.

Given a project proposal, and a subsequent spec, I don't see any fundamental reason we _wouldn't_ do this. However, the lang team would need to discuss the project proposal and determine if this would be a net win for the language, before the spec went forward.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [October 11, 2020, 5:35pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/4 "2020-10-11T17:35:36Z")

</div>

> [@tesuji](#):
>
> This should be valid Rust, but it is compilation error now:
> 
> ```rust
> 5 < 3 < true
> 
> ```

That is intentionally not valid Rust, to avoid confusing expressions like that (or, much more complex confusing expressions that include type inference). Even if we decided not to allow chained comparisons, I don't think we'd allow that.

---

<div class="post-metadata">

**Author:** ![mayabyte](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mayabyte/32/7244_2.png) [@mayabyte](https://internals.rust-lang.org/u/mayabyte)\
**Post date:** [October 11, 2020, 6:00pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/5 "2020-10-11T18:00:14Z")

</div>

> [@josh](#):
>
> `==` and `!=` should not chain, and chains with operators in opposite directions like `a < b >= c` should not work.

What makes you say this? Letting `==` and `!=` chain would be hugely helpful and I'm not seeing any issues it would cause. Operators in opposite directions may be a little unusual but also don't seem to cause any issues; `a < b >= c` would desugar into `a < b && b >= c`, which is a perfectly valid thing to write.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [October 11, 2020, 6:11pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/6 "2020-10-11T18:11:29Z")

</div>

Operators in opposite directions could have a valid desugaring, but that doesn't mean they make for clear code.

`==` and `!=` is a little more debatable, and personally I'm less attached to that one; I could probably be convinced, given some code that shows how it'd be useful. I'd be much more hesitant to support `x == y > z != a > b`, though.

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [October 11, 2020, 6:49pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/7 "2020-10-11T18:49:55Z")

</div>

> [@mayabyte](#):
>
> Operators in opposite directions may be a little unusual but also don't seem to cause any issues; `a < b >= c` would desugar into `a < b && b >= c` , which is a perfectly valid thing to write.

"Perfectly valid thing to write" seems necessary but not sufficient. I would guess `a < b >= c` is much harder to mentally parse than `a < b && b >= c`, and it's thus not clear to me the additional density is worth it for that case.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [October 11, 2020, 7:12pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/8 "2020-10-11T19:12:44Z")

</div>

I guess that depends on how often code like `a < b && b >= c` actually occurs. If one had to do this a lot then I could see this syntax be worth the complexity budget. But I don't know if that's actually the case.

---

<div class="post-metadata">

**Author:** ![mayabyte](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mayabyte/32/7244_2.png) [@mayabyte](https://internals.rust-lang.org/u/mayabyte)\
**Post date:** [October 11, 2020, 7:24pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/9 "2020-10-11T19:24:30Z")

</div>

> [@jjpe](#):
>
> I guess that depends on how often code like `a < b && b >= c` actually occurs.

That's a good point. The most common uses of this syntax I see in Python are either checking that a value lies between two others, or checking that a bunch of values are all equal to each other; mixed direction comparisons don't show up much. Not sure if this is also true in Rust, but I imagine mixed-direction chained comparisons could always be added later if there's demand for them.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [October 11, 2020, 7:39pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/10 "2020-10-11T19:39:16Z")

</div>

> [@jjpe](#):
>
> how often code like [...] actually occurs

I think this is the core of the issue. I feel like the most common cases are things where the are other acceptable ways. For example, `(a..b).contains(c)` seems like a perfectly acceptable way of writing `a <= c && c < b`. And I'm fine with `a <= b <= c <= d` not working, since I can write it fine as `[a, b, c, d].is_sorted()`. Similarly, `a < b > c` is `b > max(a, c)`, which I like as a way to write it anyway, plus it avoids weird generics-looking things.

So maybe there aren't amazing ways of writing mixes of these things, like if for some reason I needed `a <= b < c <= d`, but that case seems so unlikely that I don't care.

---

<div class="post-metadata">

**Author:** ![pdolezal](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pdolezal/32/7137_2.png) [@pdolezal](https://internals.rust-lang.org/u/pdolezal)\
**Post date:** [October 11, 2020, 7:47pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/11 "2020-10-11T19:47:17Z")

</div>

> [@josh](#):
>
> `==` and `!=` is a little more debatable, and personally I'm less attached to that one; I could probably be convinced, given some code that shows how it'd be useful. I'd be much more hesitant to support `x == y > z != a > b`, though.

I guess that `a == b == c` is the case which brings no doubts about the meaning and could be allowed. I can yet imagine `a < b == c < d` (I'd tell that it is understandable as well). On the other hand, chaining `!=` seems to me a bad idea. So, I agree that chaining `==` and `!=` should be considered very carefully.

Judging from the examples, I think that chains that look like ordered comparing interval boundaries are the case where the chaining works well and where can be useful. The other cases… I think that they are more difficult to parse for a human.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [October 11, 2020, 8:35pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/12 "2020-10-11T20:35:35Z")

</div>

This is something that can be discussed as part of a project group working on the problem, ideally with code examples for how various projects would look with this available.

---

<div class="post-metadata">

**Author:** ![Tom-Phinney](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tom-phinney/32/3299_2.png) [@Tom-Phinney](https://internals.rust-lang.org/u/Tom-Phinney)\
**Post date:** [October 11, 2020, 8:38pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/13 "2020-10-11T20:38:42Z")

</div>

So how does `f(a) < g(b) < h(c)` desugar? Recall that the arguments to each comparison [are evaluated in place expression context](https://doc.rust-lang.org/reference/expressions/operator-expr.html#comparison-operators).

Unless the `std::cmp::PartialOrd::lt` operator is overloaded to have side effects, the following probably works:

```rust
{
    let fa = f(a);
    let gb = g(b);
    let hc = h(c);
    std::cmp::PartialOrd::lt(&fa, &gb) & std::cmp::PartialOrd::lt(&gb, &hc)
}

```

---

<div class="post-metadata">

**Author:** ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)\
**Post date:** [October 11, 2020, 9:29pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/14 "2020-10-11T21:29:37Z")

</div>

> [@pdolezal](#):
>
> I guess that `a == b == c` is the case which brings no doubts about the meaning

Actually it is not 🙂

- is overloaded `==` invoked for `(a, b)` or `(b, c)` first?
- does it instead mean this?

```rust
   let a = 8;
   let b = 9;
   let c = true;
   
   (a == b) == c

```

...I've seen people write something similar in Java

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [October 11, 2020, 11:06pm UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/15 "2020-10-11T23:06:20Z")

</div>

Note: Python "desugars" this as

```rust
{
    let fa = f(a);
    let gb = g(b);
    std::cmp::PartialOrd::lt(&fa, &gb) && std::cmp::PartialOrd::lt(&gb, &h(c))
}

```

(that is, lazily evaluate `h(c)`). This directly matches "what I would write by hand" if efficiency matters, and is maybe more analogous to `Iterator::is_sorted` or `Generator::is_sorted`. The actual desugaring should cover `f(a) < g(b) < h(c) < i(d) < ...` (and be laziness all the way down).

I am in favor of this proposal. I always thought the python chained comparators were pretty intuitively reasonable; I don't think it blows the complexity budget in that regard. The motivation (make some chained comparisons a little nicer) is not super pressing, so it probably more needs a motivated person to push it all the way through.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [October 12, 2020, 12:41am UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/16 "2020-10-12T00:41:07Z")

</div>

> [@atagunov](#):
>
> ...I've seen people write something similar in Java

I've definitely done things like `(a == b) != (c == d)` in C#, because `!=` is `^`.

---

<div class="post-metadata">

**Author:** ![eminence](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eminence/32/4018_2.png) [@eminence](https://internals.rust-lang.org/u/eminence)\
**Post date:** [October 12, 2020, 12:50am UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/17 "2020-10-12T00:50:22Z")

</div>

> [@](#):
>
> ```rust
> // error: comparison operators cannot be chained 
> // help: split the comparison into two 
> // | 5 < 10 && 10 < 15 
> 
> ```

(Slightly off topic, but nice job to the compiler and team for providing an excellent diagnostic on this error)

---

<div class="post-metadata">

**Author:** ![tesuji](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tesuji/32/12790_2.png) [@tesuji](https://internals.rust-lang.org/u/tesuji)\
**Post date:** [October 12, 2020, 2:20am UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/18 "2020-10-12T02:20:12Z")

</div>

also cc [https://github.com/rust-lang/rfcs/issues/2083](https://github.com/rust-lang/rfcs/issues/2083)

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [October 12, 2020, 5:19am UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/19 "2020-10-12T05:19:43Z")

</div>

I'm against this, especially with the short-circuit behavior:

- In my experience, the problem of chained comparisons just doesn't come up. And even if it does, there's no significant inconvenience in writing `a < b && b < c`.
- So far all the comparison operators have been eager. Making them conditionally lazy requires such a mental shift that I would consider it breaking (even though it _technically_ may not be), and I definitely put it into the "too much clever terseness just for the sake of terseness" category.

---

<div class="post-metadata">

**Author:** ![kennytm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kennytm/32/161_2.png) [@kennytm](https://internals.rust-lang.org/u/kennytm)\
**Post date:** [October 12, 2020, 7:06am UTC](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191/20 "2020-10-12T07:06:14Z")

</div>

> - So far all the comparison operators have been eager. Making them conditionally lazy requires such a mental shift that I would consider it breaking (even though it _technically_ may not be), and I definitely put it into the "too much clever terseness just for the sake of terseness" category.

How could it be breaking if chained comparison did not exist.

As demonstrated before, for Python and Raku (Perl 6), in `a < b < c` when `a < b` is false the next expression `c` will _not_ be evaluated. After all, `a < b && b < c` won't evaluate `b < c` either when `a < b` is false. So always eagerly evaluating `c` _is_ the mental-shifting breaking change.

There are other languages with chained operators too.

In Julia the evaluation order is [undefined](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Chaining-comparisons) (bleh), e.g. `a < b < c` in Julia will first evaluate (eagerly) `b`, then (eagerly) `a`, and finally (lazily) `c`. This breaks the left-to-right order guarantee of Rust, but the conditional laziness is still there.

Scheme and Clojure's comparison operator can accept multiple values e.g. `(< a b c)` where all of `a`, `b` and `c` are evaluated eagerly. But `<` isn't a special operator in these Lispy languages and thus not really applicable to Rust.

[Next page](https://internals.rust-lang.org/t/python-like-chained-comparison-operators/13191.md?page=2)
