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 (=).

Example: lvalue
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 (=).

Example: rvalue
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.

OperatorNameDescription
+AdditionAdds the right operand to the left operand.
-SubtractionSubtract the right operands from the left operand.
*MultiplicationMultiply the left operand with the right operand.
/DivisionDivide the left operand with the right operand.
%ModulusDivide the left operand with the right operand and return the remainder.

Note that the ++ and – incremental operators are not supported in Rust.

Example: arithmetic operators
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.

OperatorNameExample
&Bitwise ANDBoolean AND on each bit.
|Bitwise ORBoolean OR on each bit.
^Bitwise XORExclusive boolean OR on each bit. One operand or another is true, but not both.
!Bitwise NOTUnary NOT that reverses all the bits in the operand.
<<Left BitShiftMoves bits in the left operand to the left by the amount of places specified in the right operand.
>>Right BitShiftMoves 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.

OperatorNameDescription
>Greater thanEvaluates if the left operand is greater than the right operand.
<Less thanEvaluates if the left operand is less than the right operand.
>=Greater than or equal toEvaluates if the left operand is greater than or equal to the right operand.
<=Less than or equal toEvaluates if the left operand is less than or equal to the right operand.
==EqualsEvaluates if the left operand is exactly the same as the right operand.
!=Not equalEvaluates if the left operand is not the same as the right operand.
Example: relational comparison operators
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.

OperatorDescriptionExampleSame as
=Assigna = ba = b
+=Add and assigna += ba = a+b
-=Subtract and assigna -= ba = a-b
*=Multiply and assigna *= ba = a*b
/=Divide and assigna /= ba = a/b
%=Modulo and assigna %= ba = a%b
Example: assignment operators
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.

OperatorNameDescription
&&AndReturns true if both expressions evaluate to true.
||OrRetrurns true if one or the other expression evaluate to true.
!NotReturns true if the expression immediately following it is not true.
Example: logical operators
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 ||.