# 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`

- Logical/Bitwise NOT -
- 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 -
`= += -= *= /= %= &= |= ^= <<= >>=`

- Typecast -

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

# Associativity

If two or more operators of the same precedence appear in a statement, then which operator will be evaluated first is defined by the associativity.

# Left to Right Associativity

Left associativity occurs when an expression is evaluated from left to right. An expression such as` a ~ b ~ c`

, in this case, would be interpreted as `(a ~ b) ~ c`

where `~ `

can be any operator. The operators below can be chained as left associative.

📝The comparison, assignment, and the range operator cannot be chained at all.

# Example 1

The example below solves an expression according to its operator precedence:

```
fn main() {
println!("Answer : {}",( 3 + 5 ) * 9 / 7 & 8);
}
```

output:-

```
Answer : 8
```

# Example 2

The example below solves an expression according to its operator precedence:

```
fn test() {
println!("{}", 2 + 3 / 5 ^ 7 & 8 | 9);
}
```

output:

```
11
```

# Quiz

Test your understanding of operator precedence in Rust!

- What is the output of the following code according to its operator precedence in Rust?

```
fn main() {
println!("{}", 3 + 4 - 9 / 6 * 6 ^ 8 & 3);
}
```

A) 1

B) 2

C) 3

D) 4