Operators
In this Rust tutorial we learn more about symbols in Rust that have special meaning to the compiler and allow us to perform operations. We cover arithmetic, bitwise, realtional comparison, assignment and logical operators as well as operands, lvalues and rvalues.
What is an operator
An operator is one or more symbols in Rust that have special meaning to the compiler and allow us to perform various operations.
As an example, let’s consider the + symbol. The + symbol is an addition operator and can perform arithmetic on two or more numeric values, but it can also concatenate strings and arrays.
The following types of operators are available in Rust.
- Arithmetic
- Bitwise
- Comparison
- Conditional
- Logical
Note that you don’t need to memorize these operators. We will be using them throughout the course, which will allow you to get used to them easily.
Operands - Lvalues & Rvalues
There are two kinds of expressions in Rust, Lvalues and Rvalues. They are known as the left operand and right operand respectively, or operands when we’re referring to them in general.
An lvalue is the operand that can have a value assigned to it, such as a variable. It’s allowed to appear on the left or right side of the assignment operator (=).
fn main() {
// lvalue (num1) may be
// on the left of the =
let num1 = 10;
// lvalue may also be on
// the right of the =
let num2 = num1;
}
An rvalue is the operand that is the value being assigned. It’s only allowed to appear on the right side of an assignment operator (=).
fn main() {
// rvalue (10) may only
// be on the right of the =
let num1 = 10;
// an rvalue cannot be
// on the left of the =
10 = 3;
}
Arithmetic operators
Arithmetic operators allow us to perform arithmetics on operands.
The following table lists the arithmetic operators available in Rust.
Operator | Name | Description |
---|---|---|
+ | Addition | Adds the right operand to the left operand. |
- | Subtraction | Subtract the right operands from the left operand. |
* | Multiplication | Multiply the left operand with the right operand. |
/ | Division | Divide the left operand with the right operand. |
% | Modulus | Divide the left operand with the right operand and return the remainder. |
Note that the ++ and – incremental operators are not supported in Rust.
fn main() {
// addition
println!("5 + 3 = {}", 5 + 3);
// subtraction
println!("5 - 3 = {}", 5 - 3);
// multiplication
println!("5 * 3 = {}", 5 * 3);
// division
println!("5 / 3 = {}", 5 / 3);
// modulo
println!("5 % 3 = {}", 5 % 3);
}
Bitwise operators
Bitwise operators allow us to perform bitwise operations in memory.
The following table lists the bitwise operators available in Rust.
Operator | Name | Example |
---|---|---|
& | Bitwise AND | Boolean AND on each bit. |
| | Bitwise OR | Boolean OR on each bit. |
^ | Bitwise XOR | Exclusive boolean OR on each bit. One operand or another is true, but not both. |
! | Bitwise NOT | Unary NOT that reverses all the bits in the operand. |
<< | Left BitShift | Moves bits in the left operand to the left by the amount of places specified in the right operand. |
>> | Right BitShift | Moves bits in the right operand to the right by the amount of places specified in the right operand. |
Relational (Comparison) operators
We use relational operators to compare two or more values. A relational comparison operator will return a boolean true or false.
The following table lists the relational comparison operators available in Rust.
Operator | Name | Description |
---|---|---|
> | Greater than | Evaluates if the left operand is greater than the right operand. |
< | Less than | Evaluates if the left operand is less than the right operand. |
>= | Greater than or equal to | Evaluates if the left operand is greater than or equal to the right operand. |
<= | Less than or equal to | Evaluates if the left operand is less than or equal to the right operand. |
== | Equals | Evaluates if the left operand is exactly the same as the right operand. |
!= | Not equal | Evaluates if the left operand is not the same as the right operand. |
fn main() {
// greater than
println!("5 > 3 = {}", 5 > 3);
// less than
println!("5 < 3 = {}", 5 < 3);
// greater than or equal to
println!("3 >= 3 = {}", 3 >= 3);
// less than or equal to
println!("5 <= 3 = {}", 5 <= 3);
// equals
println!("5 == 5 = {}", 5 == 5);
// not equals
println!("5 != 3 = {}", 5 != 3);
}
Assignment operators
We use assignment operators to assign whatever is on the right, to whatever is on the left.
The following table lists the assignment operators available in Rust.
Operator | Description | Example | Same as |
---|---|---|---|
= | Assign | a = b | a = b |
+= | Add and assign | a += b | a = a+b |
-= | Subtract and assign | a -= b | a = a-b |
*= | Multiply and assign | a *= b | a = a*b |
/= | Divide and assign | a /= b | a = a/b |
%= | Modulo and assign | a %= b | a = a%b |
fn main() {
// assignment
let mut a = 5;
println!("a: {}", a);
// add and assign
a += 3;
println!("a += 3 (a = a + 3): {}", a);
// subtract and assign
a -= 3;
println!("a += 3 (a = a - 3): {}", a);
// multiply and assign
a *= 3;
println!("a *= 3 (a = a * 3): {}", a);
// divide and assign
a /= 3;
println!("a /= 3 (a = a / 3): {}", a);
// modulo and assign
a %= 2;
println!("a %= 3 (a = a % 3): {}", a);
}
Logical operators
We use logical operators to combine conditional evaluations. The result is a boolean true or false.
The following table lists the logical operators available in Rust.
Operator | Name | Description |
---|---|---|
&& | And | Returns true if both expressions evaluate to true. |
|| | Or | Retrurns true if one or the other expression evaluate to true. |
! | Not | Returns true if the expression immediately following it is not true. |
fn main() {
// Logical AND &&
println!("5 >= 3 && 1 <= 2: {}", 5 >= 3 && 1 <= 2);
// Logical OR ||
println!("5 == 3 || 1 <= 2: {}", 5 == 3 || 1 <= 2);
// Logical NOT !
println!("!true: {}", !true);
}
Summary: Points to remember
- An operator is a symbol, or a combination of symbols that have a special meaning to the compiler.
- Operands are the lvalues and rvalues that operators operate on.
- We perform arithmetics with arithmetic operators such as + or %.
- We compare values with relational operators such as > or ==.
- We assign, and compound assign with assignment operators such as = and +=.
- We combine conditional evaluations with logical operators such as && or ||.