E0802: Chained comparison
| Code | Category | Stability |
|---|---|---|
E0802 | Literal and operator | Permanent |
Explanation
Comparison operators cannot be chained. They share one precedence level and associate to the left, so a < b < c would parse as (a < b) < c and compare a boolean against c rather than testing an ordering. The parser rejects the chain syntactically whenever a comparison expression is directly the left operand of another comparison; explicit parentheses break the chain, so (a < b) == c stays an ordinary boolean equality and is typed like any other.
Likely cause
A range test was written in mathematical notation, such as low < value < high. Combine two complete comparisons with &&, or parenthesize the boolean operand when a comparison against a boolean is what was actually meant.
Examples
Chain two comparisons
fn main() -> i32 {
let a = 1;
let b = 2;
let c = 3;
if a < b < c { 1 } else { 0 }
}
Combine comparisons with a logical operator
fn main() -> i32 {
let a = 1;
let b = 2;
let c = 3;
if a < b && b < c { 1 } else { 0 }
}