//
In programming, operators are symbols or keywords that perform operations on variables and values. Rust, like many other programming languages, provides a variety of operators to handle arithmetic, comparison, and logical operations. Understanding these operators is fundamental to writing effective and efficient Rust code.
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
+): Adds two operands.-): Subtracts the second operand from the first.*): Multiplies two operands./): Divides the first operand by the second. Note that division of integers truncates the result towards zero.%): Returns the remainder of the division of the first operand by the second.Comparison operators are used to compare two values and return a boolean result (true or false).
==): Checks if the two operands are equal.!=): Checks if the two operands are not equal.>): Checks if the first operand is greater than the second.<): Checks if the first operand is less than the second.>=): Checks if the first operand is greater than or equal to the second.<=): Checks if the first operand is less than or equal to the second.Logical operators are used to combine multiple conditions and return a boolean result.
&&): Returns true if both operands are true.||): Returns true if at least one of the operands is true.!): Inverts the boolean value of its operand. If the operand is true, it returns false, and vice versa.Let's explore each type of operator with practical examples.
fn main() {
let a = 10;
let b = 3;
println!("Addition: {} + {} = {}", a, b, a + b); // OutputBlock{`Addition: 10 + 3 = 13`}
println!("Subtraction: {} - {} = {}", a, b, a - b); // OutputBlock{`Subtraction: 10 - 3 = 7`}
println!("Multiplication: {} * {} = {}", a, b, a * b); // OutputBlock{`Multiplication: 10 * 3 = 30`}
println!("Division: {} / {} = {}", a, b, a / b); // OutputBlock{`Division: 10 / 3 = 3`}
println!("Modulus: {} % {} = {}", a, b, a % b); // OutputBlock{`Modulus: 10 % 3 = 1`}
}
### Comparison Operators
```rust
fn main() {
let x = 5;
let y = 10;
println!("x == y: {}", x == y); // OutputBlock{`x == y: false`}
println!("x != y: {}", x != y); // OutputBlock{`x != y: true`}
println!("x > y: {}", x > y); // OutputBlock{`x > y: false`}
println!("x < y: {}", x < y); // OutputBlock{`x < y: true`}
println!("x >= y: {}", x >= y); // OutputBlock{`x >= y: false`}
println!("x <= y: {}", x <= y); // OutputBlock{`x <= y: true`}
}
### Logical Operators
```rust
fn main() {
let p = true;
let q = false;
println!("p && q: {}", p && q); // OutputBlock{`p && q: false`}
println!("p || q: {}", p || q); // OutputBlock{`p || q: true`}
println!("!p: {}", !p); // OutputBlock{`!p: false`}
println!("!q: {}", !q); // OutputBlock{`!q: true`}
}
## What's Next?
In the next section, we will explore control flow in Rust, which includes conditional statements and loops. Understanding how to control the flow of your program is crucial for building more complex applications.
Stay tuned!