Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Rust Operators

Rust Operators

In Rust, whether it's simple numerical computation, logical judgment, or more complex pattern matching and bit operations, operators play a core role.

Rust supports the common operators of the C-family languages we're familiar with, and also provides some unique operators. 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

)

;

}

Output:

a + b = 13

a - b = 7

a * b = 30

a / b = 3

a % b = 1

Rust has no exponentiation operator like ** or ^ (note: ^ is bitwise XOR); to exponentiate, you need to use the built-in pow or powf methods:

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
== Equality 5 == 5 true
!= Inequality 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
%= Modulo and assign x %= 2 x = x % 2
&= \|= ^= <<= >>= Bitwise compound assignment x &= 2 Similar to

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 (excluding the right end) 0..5 Produces 0 to 4
..= Range (including the right end) 0..=5 Produces 0 to 5
as Type Conversion 5 as f32
? Error propagation (in Result ) some()?;
* Dereferencing *ptr
& Take a reference &x
ref Bind as a 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