Rust Tutorial
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

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
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Miscellaneous operators

Rust Arithmetic operators

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

+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
%ModuloReturns remainder of division operation


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.

!= Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to


Rust Logical operators

Logical operators are used to combine two or more conditions.

Operator NameDescription
and Logical ANDReturns true when all conditions are true
orLogical ORReturns true when any of the conditions is true
notLogical NOTReturns true when given conditions is not true

More Info

Rust Bitwise operators

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

OperatorNameDescriptionMore Info
&ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0.More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0.More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0.More Info
~NOTReverse all the bits.More Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operand.More Info
<<Left shiftThe left operand value is moved left by the number of bits present in the right operand.More Info

Rust Miscellaneous operators

The below table describes other operators supported by Rust:

ternary operator (?:)Returns one of the two values based on value of boolean expression.

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 + 52 = 28 and 3 × 52 = 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.

18Method calls
17Field expressionsLeft to Right
16Function calls, Array indexingNA
15?Question mark operator
14-aUnary minus
!Bitwise or Logical NOT
*Dereference operator
&Shared borrow operator
&mutMutable borrow operator
13asType casting keywordLeft 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 InequalityRequire parentheses
<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equal
5&&Logical ANDLeft to Right
4||Logical OR
3..  ..=Range literal, Assignment by range literalRequire parentheses
2=Direct assignmentLeft to Right
+=  -=  *=  /=  %=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
1returnreturn statementNA
breakbreak statement