Rust Reference ยท Operators and Expressions
Rust operator precedence and associativity
Reference table of Rust operator precedence and associativity from method calls down to assignment, with the traps: as, ranges, comparisons and bitwise operators.
Precedence decides how an expression without parentheses is grouped: in 1 + 2 * 3 the * binds first. Associativity decides the grouping among operators of the same level: 10 - 4 - 3 is (10 - 4) - 3 because - is left-associative. Rust's table is close to C's, with a few deliberate differences that make common code less error-prone.
The most important differences: the bitwise operators &, ^, | bind tighter than comparisons (so x & 1 == 0 works as intended), comparison operators cannot be chained, as binds tighter than arithmetic, and ranges and assignments sit near the bottom. When in doubt, add parentheses; they cost nothing and the formatter keeps them.
let a = 1 << 2 + 3; // 1 << (2 + 3) = 32 let b = x & 1 == 0; // (x & 1) == 0 let c = -2_i32.pow(2); // -(2.pow(2)) = -4
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
Highest precedence first. Operators on one row share a level.
| Level | Operators | Associativity |
|---|---|---|
| 1 | paths a::b |
|
| 2 | method calls x.f(), field access x.y, x.await |
left to right |
| 3 | function calls f(), indexing x[i] |
|
| 4 | ? |
|
| 5 | unary -, *, !, &, &mut |
|
| 6 | as |
left to right |
| 7 | *, /, % |
left to right |
| 8 | +, - |
left to right |
| 9 | <<, >> |
left to right |
| 10 | & |
left to right |
| 11 | ^ |
left to right |
| 12 | | |
left to right |
| 13 | ==, !=, <, >, <=, >= |
requires parentheses (no chaining) |
| 14 | && |
left to right |
| 15 | || |
left to right |
| 16 | .., ..= |
requires parentheses |
| 17 | =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= |
right to left |
| 18 | return, break, closures |x| ... |
Edge cases: the closure and return forms extend as far to the right as possible, so |x| x + 1 includes the + 1. A let condition (if let ... = e && ...) follows its own rules for && chains in the 2024 edition.
Usage notes
- Unary minus and methods:
-2_i32.pow(2)is-4because method calls bind tighter than unary minus. Write(-2_i32).pow(2)for4. asis high:x as f64 / 2.0castsxfirst. Butx as usize < yis a parse error because<after a type looks like generic arguments; wrap the cast in parentheses.- No C-style chaining:
a < b < canda == b == care compile errors, which removes a classic bug. Use&&. - Shifts below arithmetic:
1 << n - 1is1 << (n - 1). Mask expressions like(1 << n) - 1need the parentheses. &and|versus&&and||: the double forms short-circuit and are weaker; the single forms onboolalways evaluate both sides.- Ranges are weak:
a..b + 1isa..(b + 1);..has no associativity, soa..b..cis an error. Put a range in parentheses before a method call:(0..10).sum::<i32>()(without them the.sum()applies to10). - Blocks and statements: at the start of a statement, an expression like
{ 1 } + 2is a block statement followed by+2; wrap in parentheses when the block is the left operand. - Formatting tools such as
rustfmtnever change grouping, andclippy::precedencewarns about confusing mixes of shifts and arithmetic.