Rust Operators
Operators are symbols that perform actions on values. A plus sign adds, a double equals sign compares, and an exclamation mark flips a true value to false. Rust groups them into a few families, and each family follows strict type rules that prevent silent mistakes.
Arithmetic Operators
These operators handle everyday math.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 2 | 9 |
| - | Subtraction | 7 - 2 | 5 |
| * | Multiplication | 7 * 2 | 14 |
| / | Division | 7 / 2 | 3 |
| % | Remainder | 7 % 2 | 1 |
Integer Division
Dividing two integers drops the decimal part. The expression 7 / 2 gives 3, not 3.5. Write at least one value as a float to keep the fraction.
let a = 7 / 2; // 3
let b = 7.0 / 2.0; // 3.5Matching Types
Rust refuses to mix number types. Adding an i32 to an f64 causes a compile error. Convert one side with the as keyword.
let apples: i32 = 4;
let price: f64 = 1.5;
let total = apples as f64 * price;
println!("{}", total);
6
Comparison Operators
Comparison operators produce a bool value, which is either true or false.
==checks equality!=checks inequality<and>check smaller and larger<=and>=include equal values
let age = 18;
println!("{}", age >= 18); // true
println!("{}", age == 20); // falseLogical Operators
Logical operators combine true and false values. Think of two light switches wired in a hallway.
&& (AND) : both switches must be on || (OR) : one switch on is enough ! (NOT) : flips the switch position
let has_ticket = true;
let has_id = false;
println!("{}", has_ticket && has_id); // false
println!("{}", has_ticket || has_id); // true
println!("{}", !has_id); // trueShort-Circuit Behavior
The && operator stops when the left side is false, because the answer cannot change. The || operator stops when the left side is true. This saves work and protects against risky checks on the right side.
Compound Assignment
Compound operators update a variable in one step. The variable must be declared with mut.
let mut points = 10;
points += 5; // 15
points -= 3; // 12
points *= 2; // 24
points /= 4; // 6
points %= 4; // 2Bitwise Operators
Bitwise operators work on the individual binary digits of a number. Beginners rarely need them, yet they appear in graphics, networking, and hardware code.
| Operator | Name |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| << | Shift left |
| >> | Shift right |
let x = 0b1100;
let y = 0b1010;
println!("{:04b}", x & y); // 1000Operator Precedence
Rust follows the same order as school math. Multiplication and division run before addition and subtraction. Parentheses override the order.
let a = 2 + 3 * 4; // 14
let b = (2 + 3) * 4; // 20Overflow Protection
An u8 holds values from 0 to 255. Adding 1 to 255 overflows. Debug builds stop with a panic message, which exposes the bug early. Methods such as checked_add and wrapping_add give you control over the outcome.
let big: u8 = 255;
println!("{:?}", big.checked_add(1)); // None
println!("{}", big.wrapping_add(1)); // 0Common Mistakes
Mixing number types leads the list. Writing let total = count * 2.5; fails when count is an integer, because the types differ. Convert one side with as so both sides match.
Using a single equals sign inside a condition is another trap. A single sign assigns a value, and a double sign compares. Rust rejects if x = 5 at compile time, which saves you from a classic bug.
Expecting a decimal from integer division surprises many learners. The result of 9 / 2 is 4. Write 9.0 / 2.0 to get 4.5.
Practice Ideas
- Write a program that converts degrees Celsius to Fahrenheit using floats.
- Check whether a year is a leap year with the remainder operator and logical operators.
- Test the overflow methods on a
u8value near 255.
Key Points
- Both sides of an arithmetic operator need the same type.
- Integer division discards the fraction.
- Logical operators short-circuit.
- Use
checked_addand similar methods to handle overflow safely.
