Open In App

Rust – Operators

Last Updated : 29 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

      S.No               Operator                                                   Explanation     Example    
1 + (Addition) Returns the addition of  two operands A+B=60
2 -(Subtraction) Returns the difference of the values(subtract right operand from left) A-B=20
3 *(Multiplication) Returns the product of the values A*B=800
4 /(Division) Divide left operand by right one and returns the quotient A/B=2
5 %(Modulus) Divide left operand by right one and returns the remainder A%B=0

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

S.No                 Operator                  Explanation                                  Example                    
    > Greater than    (A>B) is True
2     < Less than    (A<B) is False
3    == Equal to    (A==B) is False
4    != Not equal to     (A!=B) is True
5   >= Greater than and equal to    (A>=B) is True
6   <= Less than and equal to     (A<=B) is False

Example:

Rust




fn main() {
    let a = 4;
    let b = 5;
     
    // performing a series of comparisons
    // using comparison 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 equal 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

    S.No          Operator                                                            Explanation                                                        Example                
1 && The operator returns true only if all the expressions specified return true (A&&B) is False
2 || The operator returns true if at least one of the expressions specified return true (A||B) is True
3 !

The operator returns the inverse of the expression’s result. For E.g.: !(>5) 

returns false !(A >10 ) is True Bitwise Operators

!(A||B) is False

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

    S.No      Operator       Explanation Example          
1 & (Bitwise AND) It performs a Boolean AND operation on each bit of its integer arguments. (A&B) is 2
2 | (BitWise OR) It performs a Boolean OR operation on each bit of its integer arguments. (A|B) is 3
3 ^ (Bitwise XOR)

It performs a Boolean exclusive OR operation on each bit of its integer arguments. 

Exclusive OR means that either operand one is true or operand two is true, but not both.

(A^B) is 1
4  ! (Bitwise Not) It is a unary operator and operates by reversing all the bits in the operand. !A is 2
5 << (Left Shift)

It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. 

Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.

(B<<1) is 6
6  >> (Right Shift) Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. (B >>1) is 1
7 >>> (Right shift with Zero) This operator is just like the >> operator, except that the bits shifted to the left are always zero. (A>>>1) is 1

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

    S.No        Operator                                 Explanation                                    Example        
+= Arithmetic addition and assignment A+=2 is 6
2 -= Arithmetic subtraction and assignment A-=2 is 2
3 *= Arithmetic multiplication and assignment A*=2 is 8
4 /= Arithmetic division and assignment A/=2 is 2
5 %= Arithmetic remainder and assignment A%=2 is 0
6 <<= Left-shift and assignment A<<=1 is 0
7 >>= Right-shift and assignment A>>=1 is 2
8 &= Bitwise AND and assignment A&=2 is 0
9 |= Bitwise OR and assignment A|=2 is 6
10 ^= Bitwise exclusive OR and assignment A^=2 is 1

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads