Precedence and Associativity

This lesson discusses two important characteristics of operators - precedence and associativity.

Precedence

The precedence of an operator determines which operation is performed first in an expression with more than one operators.

Operators are listed below in the order of their precedence from highest to lowest :

  • Unary
    • Logical/Bitwise NOT - !
    • Derereference - *
    • Borrow - &, &mut
  • Binary
    • Typecast - as
    • Multiplication- *,Division - /, Remainder - %
    • Addition -+, Subtraction - -
    • Left Shift - <<, Right Shift - >>
    • Bitwise AND - &
    • Bitwise XOR - ^
    • Bitwise OR - |
    • Comparison - == != < > <= >=
    • Logical AND - &&
    • Logical OR - ||
    • Range - start .. stop
    • Assignment/Compound Assignment - = += -= *= /= %= &= |= ^= <<= >>=

Note: The operators that are written in the same row have the same order of precedence.

Note: The range operator will be further explored while we discuss the for loop in the Loops chapter.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy