Rust Programming
An operator is a symbol that performs operations on values or variables. For example, - is an operator that performs subtraction between two values.
Rust programming provides various operators that can be categorized into the following major categories:
- Arithmetic Operators
- Compound Assignment Operators
- Logical Operators
- Comparison Operators
1. Arithmetic Operators in Rust
We use arithmetic operators to perform addition, subtraction, multiplication, and division.
Here's a list of various arithmetic operators available in Rust. We have used the variable names a and b in the example.
Operator
- + (Addition) : a + b
- - (Substraction) : a - b
- * (Multiplication) : a * b
- / (Division) : a / b
- % (Remainder) : a % b
Example 1: Addition, Subtraction and Multiplication Operators
fn main() {let a = 20;let b = 2;// add two variables using + operatorlet x = a + b;println!("{} + {} = {}", a, b, x);// subtract two variables using - operatorlet y = a - b;println!("{} - {} = {}", a, b, y);// multiply two variables using * operatorlet z = a * b;println!("{} * {} = {}", a, b, z);}{codeBox}
Output
20 + 2 = 2220 - 2 = 1820 * 2 = 40{codeBox}
Example 2: Rust Division Operator
fn main() {let dividend = 21;let divisor = 8;// arithmetic division using / operator with integerslet division = dividend / divisor;println!("{} / {} = {}", dividend, divisor, division);}{codeBox}
Output
21 / 8 = 2{codeBox}
In the above example, we use the / operator to divide two integers 21 and 8. The output of the operation is 2.
In standard calculation, 21 / 8 gives 2.625. However, in Rust, when the / operator is used with integer values, we get the quotient (integer) as the output.
If we want the actual result, we should use the / operator with floating-point values. For example,
Input
fn main() {let dividend = 21.0;let divisor = 8.0;// arithmetic division using / operator with floating point valueslet division = dividend / divisor;println!("{} / {} = {}", dividend, divisor, division);}{codeBox}
Output
21 / 8 = 2.625{codeBox}
Here, both dividend and divisor variables are assigned floating point values. Thus, the division operation returns a floating point result of 2.625.
Example 3: Remainder Operator
fn main() {let dividend = 21;let divisor = 8;// arithmetic remainder using % operatorlet remainder = dividend % divisor;println!("{} % {} = {}", dividend, divisor, remainder);}{codeBox}
Output
21 % 8 = 5{codeBox}
Here, we use the remainder operator % with two integers: 21 and 8. The output of the operation is 5.
The remainder operator %, as the name suggests, always returns the remainder after division.
Assignment Operators
In Rust, we use the assignment operator to assign a value to a variable. For example,
let mut x = 1;{codeBox}
Here, the = operator assigns the value on the right to the variable on the left.
Compound Assignment Operators
We can also use an assignment operator and an arithmetic operator, known as a compound assignment operator. For example,
let mut x = 1;// compound assignment operatorsx += 3;{codeBox}
Here, += is a compound assignment operator known as an addition assignment. It first adds 3 to the value of x (1) and assigns the final result (4) to x.
Here's a list of various compound assignment operators in Rust.
- += (Addition Assignment) : a += b ~ (a = a+b)
- -= (Substraction Assignment) : a -= b ~ (a = a-b)
- *= (Multiplication Assignment) : a *= b ~ (a = a*b)
- /= (Division Assignment) : a /= b ~ (a = a/b)
- %= (Remainder Assignment) : a %= b ~ (a = a%b)
Example: Compound Assignment Operator
fn main() {let mut a = 2;// arithmetic addition and assignmenta += 3;println!("a = {}", a);}{codeBox}
Output:
a = 5{codeBox}
Comparison Operators
We use comparison operators to compare two values or variables. For example,
6 > 5
Here, the relational operator > (greater than) checks if 6 is greater than 5.
A relational operator returns:
- true if the relation between two values is correct
- false if the relation is incorrect
> (Greater than)
- a > b
- true if a is greater than b
< (Less than)
- a < b
- true if a is less than b
>= (Greater than or equal to)
- a >= b
- true if a is greater than or equal to b
<= (Less than or equal to)
- a <= b
- true if a is less than or equal to b
== (Equal to)
- a == b
- true if a is equal to b
!= (Not equal to)
- a != b
- true if a is not equal to b
Example: Comparison Operators
fn main() {let a = 7;let b = 3;// use of comparison operatorslet c = a > b;let d = a < b;let e = a == b;println!("{} >= {} is {}", a, b, c);println!("{} <= {} is {}", a, b, d);println!("{} == {} is {}", a, b, e);}{codeBox}
Output:
7 > 3 is true7 < 3 is false7 == 3 is false{codeBox}
Logical Operators
We use logical operators to perform logical decisions or operations. A logical operation returns either true or false depending on the conditions. For example,
(5 < 6) && (7 > 4)
Here, && is the logical AND operator that returns true if both conditions are true. In our example, both conditions are true. Hence the expression is true.
There are mainly 3 logical operators in Rust.
&& (Logical AND)
- exp1 && exp2
- Returns true if both exp1 and exp2 are true
|| (Logical OR)
- exp1 || exp2
- Returns true if any one of the expressions is true
! (Logical NOT)
- !exp
- Returns true if the expression is false and returns false, if it is true
Example: Logical Operators
fn main() {let a = true;let b = false;// logical AND operationlet c = a && b;// logical OR operationlet d = a || b;// logical NOT operationlet e = !a;println!("{} && {} = {}", a, b, c);println!("{} || {} = {}", a, b, d);println!("!{} = {}", a, e);}{codeBox}
Output:
true && false = falsetrue || false = true!true = false{codeBox}