Rust Lang

Rust Operators

Like in real life, operators in the programming help us to define and perform specific functions using a programming language. Any self-respective programming language provides a set of operators that we can use to perform operations. Common operators include mathematical operators, logical operators, etc.

This guide will discuss how to work with various operators provided by the Rust programming language.

Let us get started…

R8ust Operators

Rust provides the following set of operators:

  1. Arithmetic Operators
  2. Logical Operators
  3. Comparison Operators
  4. Bitwise Operators
  5. Compound Assignment Operators

Rust Arithmetic Operators

As the name suggests, arithmetic operators allow us to perform arithmetic operations on a set of operands. Common mathematical operations include addition, subtraction, division, etc.

The following are the popular set of arithmetic operators:

Operator Symbol Operator Name Description
+ Arithmetic Addition Returns the sum of two or more operands
Arithmetic Subtraction Return the difference between two or more operands.
* Arithmetic multiplication Returns the product of two or more operands
/ Arithmetic division Returns the quotient of the left operand dividend by the right operand
% Arithmetic remainder. Returns the remainder from the division of the left operand by the right operand.

We can illustrate how to use the arithmetic operators in Rust as shown below:

fn main() {

// arithmetic operators

let x = 10;

let y = 2;

println!("Sum: {}", x + y);

println!("Differnce: {}", x - y);

println!("Product: {}", x * y);

println!("Quotient: {}", x / y);

println!("Modulo: {}", x % y);

}

The code above should return:

Rust Logical Operators

The second category of operators supported by the Rust lang is logical operators. These types of operators are used to combine two or more Boolean conditions. Logical operators will always return one Boolean value.

They include:

Operator Symbol Operator Name Description
&& Short-circuiting logical AND Returns true if all the specified conditions evaluate to be true.
|| Short-circuiting logical OR Returns true if at least one of the specified conditions is true.
! Logical NOT Negates the result of a Boolean expression. If the condition is true, the not operator returns false.

Example code implementation is as shown:

fn main() {

// arithmetic operators

let x = true;

let y = false;

println!("Logical AND: {}", (x && y));

println!("Logical OR: {}", (x || y));

println!("Logical NOT: {}", (!x));

println!("Logical NOT: {}", (!y));

}

The code above should return as:

Rust Comparison Operators

Comparison operators compare two operands and return a Boolean value based on the condition.

These operators include:

Operator Symbol Operator Name Description
> Greater than Returns true if the operand on the left is greater than the right operand.
< Less than Returns true if the left operand is less than the right operand.
>= Greater than or equal to Returns true if the left operand is greater than or equal to the right operand.
<= Less than or equal to Returns true if the left operand is less than or equal to the right operand.
== Equal to Return true if the left operand is equal right operand.
!= Not Equal to Returns true if the left operand is not equal right operand.

We can illustrate the usage of the comparison operators, as shown in the code below:

fn main() {

let x = 10;

let y = 2;

println!("is x greater than y: {}", (x > y));

println!("is x less than y: {}", (x < y));

println!("is x equal to y: {}", (x == y));

println!("is x greater than or equal to: {}", (x >= y));

println!("is x less than or equal to: {}", (x <= y));

println!("x is not equal y: {}", (x != y));

}

The code above should return:

Rust Bitwise Operators

Bitwise operators are used to performing bitwise operations. They include:

Operator Symbol Operator Name Description
& Bitwise AND Performs Boolean AND on each bit.
| Bitwise OR Perform Boolean OR on each bit
^ Bitwise XOR Performs exclusive boolean OR on each bit.
! Bitwise NOT Performs unary not.
<< Left bitshift shifts the left bit operand to the left by the amount specified by the right operand.
>> Right bitshift Shifts left operand by the value specified by the right operand.

An example source code is shown below:

fn main() {

let x = 10;

let y = 2;

println!("Bitwise AND: {}", (x & y));

println!("Bitwise OR: {}", (x | y));

println!("Bitwise Exclusive OR: {}", (x ^ y));

println!("Left Bitshift {}", (x << y));

println!("Right Bitshift: {}", (x >> y));

println!("Bitwise NOT: {}", (!x));

}

The code above should return output as shown:

Bitwise AND: 2

Bitwise OR: 10

Bitwise Exclusive OR: 8

Left Bitshift 40

Right Bitshift: 2

Bitwise NOT: -11

Rust Compound Assignment Operators

Compound assignment operators are used to assign the value on the right to the value on the left. These include:

Operator Symbol Operator Name
+= Arithmetic addition and assignment
-= Arithmetic subtraction and assignment
*= Arithmetic multiplication and assignment
/= Arithmetic division and assignment
>>= Right-shift and assignment
<<= Left-shift and assignment
%= Arithmetic remainder and assignment
&= Bitwise AND and assignment
|= Bitwise OR and assignment
^= Bitwise exclusive OR and assignment

Conclusion

This article discussed Rust operators and how we can use them in our programs. Check the Rust documentation to explore.

Thank you for reading!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list