# Rust - Operators

Operators are used to perform operation on a single operand or two operands. Operators in Rust can be categorized as follows:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators

## Rust Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

Operator | Name | Description |
---|---|---|

+ | Addition | Add two values |

- | Subtraction | Subtract one value from another |

* | Multiplication | Multiply two values |

/ | Division | Divide one value by another |

% | Modulo | Returns remainder of division operation |

Example

## Ruby Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator | Expression | Equivalent to | Description |
---|---|---|---|

= | a = 5 | a = 5 | Example |

+= | a += b | a = a + b | |

-= | a -= b | a = a - b | |

*= | a *= b | a = a * b | |

/= | a /= b | a = a / b | |

%= | a %= b | a = a % b | |

&= | a &= b | a = a & b | More Info |

|= | a |= b | a = a | b | More Info |

^= | a ^= b | a = a ^ b | More Info |

>>= | a >>= b | a = a >> b | More Info |

<<= | a <<= b | a = a << b | More Info |

## Rust Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator | Description |
---|---|

== | Equal |

!= | Not equal |

> | Greater than |

< | Less than |

>= | Greater than or equal to |

<= | Less than or equal to |

Example

## Rust Logical operators

Logical operators are used to combine two or more conditions.

Operator | Name | Description |
---|---|---|

&& | Logical AND | Returns true when all conditions are true |

|| | Logical OR | Returns true when any of the conditions is true |

! | Logical NOT | Returns true when given conditions is not true |

More Info

## Rust Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

Operator | Name | Description | More Info |
---|---|---|---|

& | AND | Returns 1 if both bits at the same position in both operands are 1, else returns 0. | More Info |

| | OR | Returns 1 if one of two bits at the same position in both operands is 1, else returns 0. | More Info |

^ | XOR | Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0. | More Info |

! | NOT | Reverse all the bits. | More Info |

>> | Right shift | The left operand is moved right by the number of bits present in the right operand. | More Info |

<< | Left shift | The left operand value is moved left by the number of bits present in the right operand. | More Info |

## Rust Operators Precedence

Operator precedence (**order of operations**) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 5^{2} = 28 and 3 × 5^{2} = 75.

The following table lists the precedence and associativity of Rust operators. Operators are listed top to bottom, in **descending precedence**. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

Precedence | Operator | Description | Associativity |
---|---|---|---|

19 | Paths | NA | |

18 | Method calls | ||

17 | Field expressions | Left to Right | |

16 | Function calls, Array indexing | NA | |

15 | ? | Question mark operator | |

14 | -a | Unary minus | |

! | Bitwise or Logical NOT | ||

* | Dereference operator | ||

& | Shared borrow operator | ||

&mut | Mutable borrow operator | ||

13 | as | Type casting keyword | Left to Right |

: | : operator (multiple uses) | ||

12 | * / % | Multiplication, Division, Remainder | |

11 | + - | Addition, Subtraction | |

10 | << >> | Bitwise left shift and right shift | |

9 | & | Bitwise or Logical AND | |

8 | ^ | Bitwise or Logical XOR | |

7 | | | Bitwise or Logical OR | |

6 | == != | Equality and Inequality | Require parentheses |

< <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal | ||

5 | && | Logical AND | Left to Right |

4 | || | Logical OR | |

3 | .. ..= | Range literal, Assignment by range literal | Require parentheses |

2 | = | Direct assignment | Right to Left |

+= -= *= /= %= | Compound assignment by sum, difference, product, quotient and remainder | ||

<<= >>= | Compound assignment by Bitwise left shift and right shift | ||

&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | ||

1 | return | return statement | NA |

break | break statement | ||

closures |