Rust Operators
In Rust, whether it is simple numeric calculations, logical judgments, or more complex pattern matching and bit operations, operators play a core role.
Rust supports both the common operators familiar from C-family languages and provides some unique operator symbols. Mastering these operators not only makes code more concise and efficient, but also helps you better understand Rust's semantics.
1. Arithmetic Operators
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 2 |
7 |
- |
Subtraction | 5 - 2 |
3 |
* |
Multiplication | 5 * 2 |
10 |
/ |
Division (integer division) | 5 / 2 |
2(integer) |
% |
Remainder | 5 % 2 |
1 |
Example
fn main() {
let a = 10;
let b = 3;
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);
}
let a = 10;
let b = 3;
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);
}
Output:
a + b = 13 a - b = 7 a * b = 30 a / b = 3 a % b = 1
Rust does not have**or^such an exponentiation operator (note:^is bitwise XOR). To perform exponentiation, you need to use the built-in pow or powf methods:
- Integer typesuse
.pow(exp: u32) - Floating-point typesuse
.powf(exp: f64)
Integer Exponentiation
Example
fn main() {
let base: i32 = 2;
let result = base.pow(3); // 2^3
println!("2^3 = {}", result);
}
let base: i32 = 2;
let result = base.pow(3); // 2^3
println!("2^3 = {}", result);
}
Output:
2^3 = 8
Floating-Point Exponentiation
Example
fn main() {
let base: f64 = 2.0;
let result = base.powf(2.5); // 2^2.5
println!("2^2.5 = {}", result);
}
let base: f64 = 2.0;
let result = base.powf(2.5); // 2^2.5
println!("2^2.5 = {}", result);
}
Output:
2^2.5 = 5.656854249492381
2. Relational (Comparison) Operators
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal | 5 == 5 |
true |
!= |
Not equal | 5 != 2 |
true |
> |
Greater than | 5 > 2 |
true |
< |
Less than | 5 < 2 |
false |
>= |
Greater than or equal to | 5 >= 5 |
true |
<= |
Less than or equal to | 2 <= 5 |
true |
Example
fn main() {
let x = 5;
let y = 10;
println!("x == y : {}", x == y);
println!("x != y : {}", x != y);
println!("x > y : {}", x > y);
println!("x < y : {}", x < y);
println!("x >= y : {}", x >= y);
println!("x <= y : {}", x <= y);
}
let x = 5;
let y = 10;
println!("x == y : {}", x == y);
println!("x != y : {}", x != y);
println!("x > y : {}", x > y);
println!("x < y : {}", x < y);
println!("x >= y : {}", x >= y);
println!("x <= y : {}", x <= y);
}
Output:
x == y : false x != y : true x > y : false x < y : true x >= y : false x <= y : true
3. Logical Operators
| Operator | Description | Example | Result |
|---|---|---|---|
&& |
Logical AND | true && false |
false |
|| |
Logical OR | true || false |
true |
! |
Logical NOT | !true |
false |
Example
fn main() {
let a = true;
let b = false;
println!("a && b = {}", a && b);
println!("a || b = {}", a || b);
println!("!a = {}", !a);
}
let a = true;
let b = false;
println!("a && b = {}", a && b);
println!("a || b = {}", a || b);
println!("!a = {}", !a);
}
Output:
a && b = false a || b = true !a = false
4. Bitwise Operators
| Operator | Description | Example | Result |
|---|---|---|---|
& |
Bitwise AND | 5 & 3 |
1 |
| |
Bitwise OR | 5 | 3 |
7 |
^ |
Bitwise XOR | 5 ^ 3 |
6 |
! |
Bitwise NOT | !5 |
-6 |
<< |
Left shift | 5 << 1 |
10 |
>> |
Right shift | 5 >> 1 |
2 |
Example
fn main() {
let x: u8 = 0b1010; // 10
let y: u8 = 0b1100; // 12
println!("x & y = {:b}", x & y);
println!("x | y = {:b}", x | y);
println!("x ^ y = {:b}", x ^ y);
println!("!x = {:b}", !x);
println!("x << 1 = {:b}", x << 1);
println!("x >> 1 = {:b}", x >> 1);
}
let x: u8 = 0b1010; // 10
let y: u8 = 0b1100; // 12
println!("x & y = {:b}", x & y);
println!("x | y = {:b}", x | y);
println!("x ^ y = {:b}", x ^ y);
println!("!x = {:b}", !x);
println!("x << 1 = {:b}", x << 1);
println!("x >> 1 = {:b}", x >> 1);
}
Output:
x & y = 1000 x | y = 1110 x ^ y = 110 !x = 11110101 x << 1 = 10100 x >> 1 = 101
5. Assignment and Compound Assignment Operators
| Operator | Description | Example | Result |
|---|---|---|---|
= |
Assignment | let mut x = 5; x = 3; |
x = 3 |
+= |
Add and assign | x += 2 |
x = x + 2 |
-= |
Subtract and assign | x -= 2 |
x = x - 2 |
*= |
Multiply and assign | x *= 2 |
x = x * 2 |
/= |
Divide and assign | x /= 2 |
x = x / 2 |
%= |
Remainder and assign | x %= 2 |
x = x % 2 |
&= |= ^= <<= >>= |
Bitwise compound assignment | x &= 2 |
similar |
Example
fn main() {
let mut n = 5;
n += 3;
println!("n += 3 -> {}", n);
n *= 2;
println!("n *= 2 -> {}", n);
n >>= 1;
println!("n >>= 1 -> {}", n);
}
let mut n = 5;
n += 3;
println!("n += 3 -> {}", n);
n *= 2;
println!("n *= 2 -> {}", n);
n >>= 1;
println!("n >>= 1 -> {}", n);
}
Output:
n += 3 -> 8 n *= 2 -> 16 n >>= 1 -> 8
6. Other Common Operators
| Operator | Description | Example |
|---|---|---|
.. |
Range (exclusive of right endpoint) | 0..5produces 0 to 4 |
..= |
Range (inclusive of right endpoint) | 0..=5produces 0 to 5 |
as |
Type conversion | 5 as f32 |
? |
Error propagation (inResult) |
some()?; |
* |
Dereference | *ptr |
& |
Take reference | &x |
ref |
Bind as reference | let ref y = x; |
Example
fn main() {
let x = 5;
let y = x as f64;
for i in 1..4 {
print!("{} ", i);
}
println!();
for i in 1..=3 {
print!("{} ", i);
}
println!();
let a = 10;
let b = &a;
println!("*b = {}", *b);
}
let x = 5;
let y = x as f64;
for i in 1..4 {
print!("{} ", i);
}
println!();
for i in 1..=3 {
print!("{} ", i);
}
println!();
let a = 10;
let b = &a;
println!("*b = {}", *b);
}
Output:
1 2 3 1 2 3 *b = 10Other Extensions