Hoppa till innehållet

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.

Rust
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

Rust Grouping surprises

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.

Rust Comparison operators do not chain

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 -4 because method calls bind tighter than unary minus. Write (-2_i32).pow(2) for 4.
  • as is high: x as f64 / 2.0 casts x first. But x as usize < y is a parse error because < after a type looks like generic arguments; wrap the cast in parentheses.
  • No C-style chaining: a < b < c and a == b == c are compile errors, which removes a classic bug. Use &&.
  • Shifts below arithmetic: 1 << n - 1 is 1 << (n - 1). Mask expressions like (1 << n) - 1 need the parentheses.
  • & and | versus && and ||: the double forms short-circuit and are weaker; the single forms on bool always evaluate both sides.
  • Ranges are weak: a..b + 1 is a..(b + 1); .. has no associativity, so a..b..c is an error. Put a range in parentheses before a method call: (0..10).sum::<i32>() (without them the .sum() applies to 10).
  • Blocks and statements: at the start of a statement, an expression like { 1 } + 2 is a block statement followed by +2; wrap in parentheses when the block is the left operand.
  • Formatting tools such as rustfmt never change grouping, and clippy::precedence warns about confusing mixes of shifts and arithmetic.