Related Articles

# Rust – Operators

• Last Updated : 21 Jun, 2021

Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Rust operators. Operators tell the compiler or interpreter to perform a specific mathematical, logical, or relational operation.

The following are the types of operators in Rust:

• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Bitwise Operators
• Compound Assignment Operators

### Arithmetic Operators

Arithmetic operators are used for performing mathematical operations like addition, subtraction, multiplication, and division.

Let us assume that A=40 and B=20

Example:

## Rust

 `   ``fn` `main() {``   ``let` `x = 20 ;``   ``let` `y = 4;``   ``let` `mut` `result:i32;``   ` `   ``// addition of  two operands``   ``result = x + y;``   ``println!(``"Sum: {} "``,result);``   ` `   ``// the difference of the values``   ``result = x - y;``   ``println!(``"Difference: {} "``,result) ;``   ` `   ``// the product of the values``   ``result = x*y ;``   ``println!(``"Product: {} "``,result) ;` `   ``// Divide left operand by right one``   ``// and returns the quotient``   ``result = x/y ;``   ``println!(``"Quotient: {} "``,result);``   ` `   ``// Divide left operand by right one``   ``// and returns the remainder``   ``result = x%y ;``   ``println!(``"Remainder: {} "``,result);``}`

Output:

```Sum: 24
Difference: 16
Product: 80
Quotient: 5
Remainder: 0 ```

### Comparison Operators

Comparison operators are the operators that compare values and return true or false depending upon the conditions.

Let us assume that A=40 and B=20

Example:

## Rust

 `fn` `main() {``    ``let` `a = 4;``    ``let` `b = 5;``    ` `    ``// performing a series of comparisions``    ``// using comparision operators``    ` `    ``// is equal to``    ``let` `c = a == b;``    ` `    ``// is not equal to``    ``let` `d = a != b;``    ` `    ``// is less than``    ``let` `e = a < b;``    ` `    ``// is greater than``    ``let` `f = a > b;``    ` `    ``// is less than or equa to``    ``let` `g = a <= a;``    ` `    ``// is greater than or equal to``    ``let` `h = a >= a;``    ` `    ``println!("a: {}, b: {},``    ``c: {0} == {1} is {},``    ``d: {0} != {1} is {},``    ``e: {0}<{1} is {},``    ``f: {0}>{1} is {},``    ``g: {0}<={0} is {},``    ``h: {0}>={0} is {}",``    ``a, b, c, d, e, f, g, h);``}`

Output:

```a: 4, b: 5,
c: 4 == 5 is false,
d: 4 != 5 is true,
e: 4<5 is true,
f: 4>5 is false,
g: 4<=4 is true,
h: 4>=4 is true```

### Logical Operators

Logical Operators are used to combine two or more conditions. Logical operators too, return a Boolean value.

Let us assume that A=1 and B=0

Example:

## Rust

 `fn` `main() {``    ``let` `a = ``false``;``    ``let` `b = ``true``;` `    ``let` `c = !a;``    ``let` `d = a && b;``    ``let` `e = a || b;``    ` `    ``println!("``    ``a: {}, b: {},``    ``c: !{0} is {},``    ``d: {0} && {1} is {},``    ``e: {0} || {1} is {}",``    ``a, b, c, d, e);``}`

Output:

```a: false, b: true,
c: !false is true,
d: false && true is false,
e: false || true is true```

### Bitwise Operators

Bitwise is a level of operations that involves working with individual bits, which are the smallest units of data in a computer. Each bit has a single binary value: 0 or 1.

Let us assume that A=2 and B=3

Example:

## Rust

 `fn` `main() {``    ``let` `a = 1;``    ``let` `b = 2;` `      ``// Bitwise AND, 0  (01 && 10 -> 00)``    ``let` `c = a & b;``  ` `    ``// Bitwise OR, 3  (01 || 10 -> 11) ``    ``let` `d = a | b; ``  ` `      ``// Bitwise exclusive OR, 3  (01 != 10 -> 11)``    ``let` `e = a ^ b;``  ` `      ``// Left-shift, 4 ``    ``let` `f = a << b;``  ` `      ``// 16 a->'01'+"0000" ->'10000'``    ``let` `f2 = a << 4;``  ` `      ``// Right-shift, 0``    ``let` `g = a >> b;``  ` `      ``// 0 '11111' -> remove 4 bits from the end ->'1'``    ``let` `g2 = 31 >> 4;``  ` `      ``// Bitwise NOT``    ``let` `h = !a;` `    ``println!("``    ``a: {a}, b: {b},``    ``c: {a} & {b} is {c},``    ``d: {a} | {b} is {d},``    ``e: {a} ^ {b} is {e},``    ``f: {a} << {b} is {f},``    ``f2: {a} << 4 is {f2},``    ``g: {a} >> {b} is {g},``    ``g2: {a} >> {b} is {g2},``    ``h: !{a} = {h}",``    ``a=a, b=b, c=c, d=d, e=e, f=f, f2=f2, g=g, g2=g2, h=h);``    ``}`

Output:

```a: 1, b: 2,
c: 1 & 2 is 0,
d: 1 | 2 is 3,
e: 1 ^ 2 is 3,
f: 1 << 2 is 4,
f2: 1 << 4 is 16,
g: 1 >> 2 is 0,
g2: 1 >> 2 is 1,
h: !1 = -2```

### Compound Assignment Operators

Compound-assignment operators perform the operation specified by the additional operator, then assign the result to the left operand. For example, a compound-assignment expression such as

`expression1 += expression2`

Let us assume that A=4

Example:

## Rust

 `fn` `main() {``    ``let` `mut` `a = 4;``    ` `    ``println!(``"a is {}"``, a);``    ` `    ``// Arithmetic addition and assignment, 4 + 5 = 7``    ``a += 5;``    ``println!(``"1: a += 5 is {}"``, a);``    ` `    ``// Arithmetic subtraction and assignment, 9 - 2 = 7``    ``a -= 2;``    ``println!(``"2: a -= 2 is {}"``, a);``    ` `    ``// Arithmetic multiplication and assignment, 7 * 5 = 35``    ``a *= 5;``    ``println!(``"3: a *= 5 is {}"``, a);``    ` `    ``// Arithmetic division and assignment, 35 / 2 = 17 not 17.5``    ``a /= 2;``    ``println!(``"4: a /= 2 is {}"``, a);``    ` `    ``// Arithmetic remainder and assignment, 17 % 5 = 2``    ``a %= 5;``    ``println!(``"5: a %= 5 is {}"``, a);``    ` `    ``// Bitwise AND and assignment, 10 && 10 -> 10 -> 2``    ``a &= 2;``    ``println!(``"6: a &= 2 is {}"``, a);``    ` `    ``// Bitwise OR and assignment, 010 || 101 -> 111 -> 7``    ``a |= 5;``    ``println!(``"7: a |= 5 is {}"``, a);``    ` `    ``// Bitwise exclusive OR and assignment, 111 ^= 010 -> 101 -> 5``    ``a ^= 2;``    ``println!(``"8: a ^= 2 is {}"``, a);``    ` `    ``// Left-shift and assignment, '101'+'0' -> 1010 -> 10``    ``a <<= 1;``    ``println!(``"9: a <<= 1 is {}"``, a);``    ` `    ``// Right-shift and assignment, 101̶0̶ -> 10 ->``    ``a >>= 2;  2``    ``println!(``"10: a >>= 2 is {}"``, a);``}`

Output:

```a is 4
1: a += 5 is 9
2: a -= 2 is 7
3: a *= 5 is 35
4: a /= 2 is 17
5: a %= 5 is 2
6: a &= 2 is 2
7: a |= 5 is 7
8: a ^= 2 is 5
9: a <<= 1 is 10
10: a >>= 2 is 2```

My Personal Notes arrow_drop_up