Rust Operators
Operators are symbols that perform an action on values, such as adding two numbers or comparing them. Rust's operators fall into a few groups: arithmetic, assignment, comparison, logical and bitwise.
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 3 |
10 |
- |
Subtraction | 7 - 3 |
4 |
* |
Multiplication | 7 * 3 |
21 |
/ |
Division | 7 / 3 |
2 |
% |
Remainder | 7 % 3 |
1 |
Pay attention to division: when both sides are integers, the result is an integer and the fractional part is thrown away. Use floating-point numbers if you need the decimals.
fn main() {
let a = 17;
let b = 5;
println!("{} + {} = {}", a, b, a + b);
println!("{} - {} = {}", a, b, a - b);
println!("{} * {} = {}", a, b, a * b);
println!("{} / {} = {}", a, b, a / b);
println!("{} % {} = {}", a, b, a % b);
println!("17.0 / 5.0 = {}", 17.0 / 5.0);
}17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2
17.0 / 5.0 = 3.4
Both operands must have the same type. 5 + 2.5 does not compile; you must convert first, for example 5 as f64 + 2.5.
Note: Rust has no
++or--operators. Writex += 1orx -= 1instead.
Powers are not an operator either — they are methods: 2i32.pow(10) for integers and 2.0f64.powf(0.5) for floats.
Assignment operators
Compound assignment operators update a mutable variable in place:
| Operator | Same as |
|---|---|
x = 5 |
assign 5 |
x += 3 |
x = x + 3 |
x -= 3 |
x = x - 3 |
x *= 3 |
x = x * 3 |
x /= 3 |
x = x / 3 |
x %= 3 |
x = x % 3 |
fn main() {
let mut balance = 100;
balance += 50; // deposit
println!("After deposit: {}", balance);
balance -= 30; // withdrawal
println!("After withdrawal: {}", balance);
balance *= 2; // lucky bonus
println!("After bonus: {}", balance);
balance /= 4; // split four ways
println!("Each share: {}", balance);
}After deposit: 150
After withdrawal: 120
After bonus: 240
Each share: 60
Comparison operators
Comparisons produce a bool — true or false.
| Operator | Meaning |
|---|---|
== |
equal to |
!= |
not equal to |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
Logical operators
| Operator | Name | True when |
|---|---|---|
&& |
AND | both sides are true |
|| |
OR | at least one side is true |
! |
NOT | the value is false |
&& and || are short-circuiting: if the left side already decides the answer, the right side is not evaluated.
fn main() {
let temperature = 23;
let raining = false;
println!("Warm? {}", temperature > 20);
println!("Exactly 23? {}", temperature == 23);
println!("Not 30? {}", temperature != 30);
let picnic = temperature >= 18 && !raining;
println!("Picnic weather? {}", picnic);
let stay_inside = raining || temperature < 5;
println!("Stay inside? {}", stay_inside);
}Warm? true
Exactly 23? true
Not 30? true
Picnic weather? true
Stay inside? false
Bitwise operators
These work on the individual bits of integers. You will meet them in low-level code, flags and puzzles.
| Operator | Meaning | 12 op 10 |
|---|---|---|
& |
AND | 8 |
| |
OR | 14 |
^ |
XOR | 6 |
<< |
shift left | 12 << 1 = 24 |
>> |
shift right | 12 >> 2 = 3 |
Operator precedence
Just like in maths, *, / and % are applied before + and -. Comparisons come after arithmetic, then &&, then ||. When in doubt, add parentheses — they make your intent obvious:
fn main() {
let without = 2 + 3 * 4;
let with = (2 + 3) * 4;
println!("2 + 3 * 4 = {}", without);
println!("(2 + 3) * 4 = {}", with);
println!("2 to the 10th = {}", 2i32.pow(10));
}2 + 3 * 4 = 14
(2 + 3) * 4 = 20
2 to the 10th = 1024
Test yourself
What is the result of 9 / 2 in Rust when both values are integers?
- 4.5
- 4
- 5
- Compile error
How do you increase a mutable variable count by one?
- count++
- ++count
- count += 1
- inc(count)
What does true && !true evaluate to?
- true
- false
- It does not compile
- 1