Rust – Operators
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 are used for performing mathematical operations like addition, subtraction, multiplication, and division.
Let us assume that A=40 and B=20
|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|
Sum: 24 Difference: 16 Product: 80 Quotient: 5 Remainder: 0
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
|1||>||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|
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 are used to combine two or more conditions. Logical operators too, return a Boolean value.
Let us assume that A=1 and B=0
|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|
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|
a: false, b: true, c: !false is true, d: false && true is false, e: false || true is true
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
|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|
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
|1||+=||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|
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