Rust Operators
In Rust, whether it's simple numerical calculations, logical judgments, or more complex pattern matching and bitwise operations, operators play a core role.
Rust not only supports the common operators familiar to us from C-style languages, but also provides some unique operators. Mastering these operators will not only make your code more concise and efficient, but also help 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);
}
**Output:**
a + b = 13 a - b = 7 a * b = 30 a / b = 3 a % b = 1
Rust does not have exponentiation operators like ** or ^ (Note: ^ is bitwise XOR). If you need to perform exponentiation, you must use the built-in pow or powf methods:
* **Integer types** use `.pow(exp: u32)`
* **Floating-point types** use `.powf(exp: f64)`
### Integer Exponentiation
## Example
fn main(){
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);
}
Output:
2^2.5 = 5.656854249492381
* * *
## 2. Relational (Comparison) Operators
| Operator | Description | Example | Result |
| --- | --- | --- | --- |
| `==` | Equal to | `5 == 5` | `true` |
| `!=` | Not equal to | `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);
}
**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);
}
**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);
}
**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);
}
**Output:**
n += 3 -> 8 n *= 2 -> 16 n >>= 1 -> 8
* * *
## 6. Other Common Operators
| Operator | Description | Example |
| --- | --- | --- |
| `..` | Range (exclusive of the right end) | `0..5` yields 0 to 4 |
| `..=` | Range (inclusive of the right end) | `0..=5` yields 0 to 5 |
| `as` | Type casting | `5 as f32` |
| `?` | Error propagation (in `Result`) | `some()?;` |
| `*` | Dereference | `*ptr` |
| `&` | Borrow 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);
}
**Output:**
1 2 3 1 2 3 *b = 10
YouTip