Operators in Rust
In this lesson, you will learn about operators in Rust. Operators are symbols used to perform operations on values and variables.
Rust provides a rich set of operators for arithmetic calculations, comparisons, logical decisions, and assignment.
What Is an Operator?
An operator is a special symbol that performs a specific action on one or more operands.
Operands are the values or variables on which the operator works.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Rust supports the following arithmetic operators:
+Addition-Subtraction*Multiplication/Division%Remainder (modulus)
fn main() {
let a = 10;
let b = 3;
println!("{}", a + b);
println!("{}", a - b);
println!("{}", a * b);
println!("{}", a / b);
println!("{}", a % b);
}
When dividing integers, Rust performs integer division and discards the decimal part.
Assignment Operators
Assignment operators are used to assign values to variables.
The basic assignment operator is =.
Rust also provides compound assignment operators.
=+=-=*=/=%=
fn main() {
let mut x = 10;
x += 5;
x -= 3;
x *= 2;
println!("{}", x);
}
Compound assignment operators update the variable in place.
Comparison Operators
Comparison operators are used to compare two values.
They return a boolean value: true or false.
==Equal to!=Not equal to>Greater than<Less than>=Greater than or equal to<=Less than or equal to
fn main() {
let a = 5;
let b = 10;
println!("{}", a == b);
println!("{}", a != b);
println!("{}", a < b);
println!("{}", a >= b);
}
Comparison operators are commonly used in conditions and control flow.
Logical Operators
Logical operators are used to combine boolean expressions.
&&Logical AND||Logical OR!Logical NOT
fn main() {
let x = true;
let y = false;
println!("{}", x && y);
println!("{}", x || y);
println!("{}", !x);
}
Logical operators are often used with conditional statements.
Bitwise Operators
Bitwise operators work on the binary representation of numbers. They are commonly used in low-level programming.
&Bitwise AND|Bitwise OR^Bitwise XOR<<Left shift>>Right shift
fn main() {
let a = 5; // 0101
let b = 3; // 0011
println!("{}", a & b);
println!("{}", a | b);
println!("{}", a ^ b);
println!("{}", a << 1);
println!("{}", a >> 1);
}
Operator Precedence
Operator precedence determines the order in which operations are evaluated.
Multiplication and division are evaluated before addition and subtraction. Parentheses can be used to control evaluation order.
fn main() {
let result1 = 10 + 2 * 3;
let result2 = (10 + 2) * 3;
println!("{}", result1);
println!("{}", result2);
}
📝 Practice Exercises
Exercise 1
Perform addition, subtraction, and multiplication on two numbers.
Exercise 2
Use comparison operators to compare two values.
Exercise 3
Use logical operators to combine two boolean values.
Exercise 4
Use a compound assignment operator to update a variable.
✅ Practice Answers
Answer 1
fn main() {
let a = 8;
let b = 4;
println!("{}", a + b);
println!("{}", a - b);
println!("{}", a * b);
}
Answer 2
fn main() {
let x = 5;
let y = 10;
println!("{}", x < y);
}
Answer 3
fn main() {
let a = true;
let b = false;
println!("{}", a || b);
}
Answer 4
fn main() {
let mut x = 10;
x += 5;
println!("{}", x);
}
What’s Next?
Now that you understand operators in Rust, you are ready to learn about conditional statements.
In the next lesson, you will explore if, else,
and conditional expressions in Rust.