MOCKSTACKS
EN  # RUST Operators

RUST operators commonly used can be defined as 3 categories:
• Arithmetic
• Relational
• Logical

## Arithmetic Operators

Operator Description
+ (Addition) returns the sum of the operands
- (Substraction) returns the difference of the values
* (Multiplication) returns the product of the values
/ (Division) performs division operation and returns the quotient
% (Modulo) performs division operation and returns the remainder

### RUST Example of Arithmetic Operators

``````fn main() {
let num1 = 10 ;
let num2 = 2;
let mut result:i32;
result = num1 + num2;
println!("Sum: {} ",result);
result = num1 - num2;
println!("Difference: {} ",result) ;
result = num1*num2 ;
println!("Product: {} ",result) ;
result = num1/num2 ;
println!("Quotient: {} ",result);
result = num1%num2 ;
println!("Remainder: {} ",result);
}
``````

Sum: 12
Difference: 8
Product: 20
Quotient: 5
Remainder: 0

## Relational Operators

Operator Description
> Greater than
< Lesser than
>= Greater than or equal to
<= Lesser than or equal to
== is Equal
!= is not Equal

### RUST Example of Relational Operators

``````fn main() {
let A:i32 = 10;
let B:i32 = 20;
println!("Value of A:{} ",A);
println!("Value of B : {} ",B);
let mut res = A>B ;
println!("A greater than B: {} ",res);
res = A<B ;
println!("A lesser than B: {} ",res) ;
res = A>=B ;
println!("A greater than or equal to B: {} ",res);
res = A<=B;
println!("A lesser than or equal to B: {}",res) ;
res = A==B ;
println!("A is equal to B: {}",res) ;
res = A!=B ;
println!("A is not equal to B: {} ",res);
}
``````

### Output

Value of A:10
Value of B : 20
A greater than B: false
A lesser than B: true
A greater than or equal to B: false
A lesser than or equal to B: true
A is equal to B: false
A is not equal to B: true

## Logical Operators

Operator Description
&& (And)  The operator returns true only if all the expressions specified return true
||(OR)  The operator returns true if at least one of the expressions specified return true
! (NOT) The operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false

### RUST Example of Logical Operators

``````fn main() {
let a=20;
let b=30;
if (a > 10) && (b > 10) {
println!("true");
}
let c=0;
let d=30;
if (c>10) || (d>10){
println!("true");
}
let is_elder=false;
if !is_elder {
println!("Not Elder");
}
}
``````

true
true
Not Elder

## Conclusion

In this page (written and validated by ) you learned about Rust Operators . What's Next? If you are interested in completing Rust tutorial, your next topic will be learning about: Rust If Else - Decision Making.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.