Operators
Unary operator expressions
Rust defines the following unary operators. They are all written as prefix operators, before the expression they apply to.
-
: Negation. Signed integer types and floating-point types support negation. It is an error to apply negation to unsigned types; for example, the compiler rejects-1u32
.*
: Dereference. When applied to a pointer, it denotes the pointed-to location. For pointers to mutable locations, the resulting value can be assigned to. On non-pointer types, it calls thederef
method of thestd::ops::Deref
trait, or thederef_mut
method of thestd::ops::DerefMut
trait (if implemented by the type and required for an outer expression that will or could mutate the dereference), and produces the result of dereferencing the&
or&mut
borrowed pointer returned from the overload method.!
: Logical negation. On the boolean type, this flips betweentrue
andfalse
. On integer types, this inverts the individual bits in the two's complement representation of the value.&
and&mut
: Borrowing. When applied to a value, these operators produce a reference (pointer) to that value. The value is also placed into a borrowed state for the duration of the reference. For a shared borrow (&
), this implies that the value may not be mutated, but it may be read or shared again. For a mutable borrow (&mut
), the value may not be accessed in any way until the borrow expires.
Binary operator expressions
Binary operators expressions are given in order of operator precedence.
Arithmetic operators
Binary arithmetic expressions are syntactic sugar for calls to built-in traits,
defined in the std::ops
module of the std
library. This means arithmetic
operators can be overridden for user-defined types. The default meaning of the
operators on standard types is given here.
+
: Addition and array/string concatenation. Calls theadd
method on thestd::ops::Add
trait.-
: Subtraction. Calls thesub
method on thestd::ops::Sub
trait.*
: Multiplication. Calls themul
method on thestd::ops::Mul
trait./
: Quotient. Calls thediv
method on thestd::ops::Div
trait.%
: Remainder. Calls therem
method on thestd::ops::Rem
trait.
Note that Rust does not have a built-in operator for exponential (power)
calculation; see the pow
method on the numeric types.
Bitwise operators
Like the arithmetic operators, bitwise operators are syntactic sugar for calls
to methods of built-in traits. This means bitwise operators can be overridden
for user-defined types. The default meaning of the operators on standard types
is given here. Bitwise &
, |
and ^
applied to boolean arguments are
equivalent to logical &&
, ||
and !=
evaluated in non-lazy fashion.
&
: Bitwise AND. Calls thebitand
method of thestd::ops::BitAnd
trait.|
: Bitwise inclusive OR. Calls thebitor
method of thestd::ops::BitOr
trait.^
: Bitwise exclusive OR. Calls thebitxor
method of thestd::ops::BitXor
trait.<<
: Left shift. Calls theshl
method of thestd::ops::Shl
trait.>>
: Right shift (arithmetic). Calls theshr
method of thestd::ops::Shr
trait.
Lazy boolean operators
The operators ||
and &&
may be applied to operands of boolean type. The
||
operator denotes logical 'or', and the &&
operator denotes logical
'and'. They differ from |
and &
in that the right-hand operand is only
evaluated when the left-hand operand does not already determine the result of
the expression. That is, ||
only evaluates its right-hand operand when the
left-hand operand evaluates to false
, and &&
only when it evaluates to
true
.
Comparison operators
Comparison operators are, like the arithmetic operators and bitwise operators, syntactic sugar for calls to built-in traits. This means that comparison operators can be overridden for user-defined types. The default meaning of the operators on standard types is given here.
==
: Equal to. Calls theeq
method on thestd::cmp::PartialEq
trait.!=
: Unequal to. Calls thene
method on thestd::cmp::PartialEq
trait.<
: Less than. Calls thelt
method on thestd::cmp::PartialOrd
trait.>
: Greater than. Calls thegt
method on thestd::cmp::PartialOrd
trait.<=
: Less than or equal. Calls thele
method on thestd::cmp::PartialOrd
trait.>=
: Greater than or equal. Calls thege
method on thestd::cmp::PartialOrd
trait.
Type cast expressions
A type cast expression is denoted with the binary operator as
.
Executing an as
expression casts the value on the left-hand side to the type
on the right-hand side.
An example of an as
expression:
# fn sum(values: &[f64]) -> f64 { 0.0 }
# fn len(values: &[f64]) -> i32 { 0 }
fn average(values: &[f64]) -> f64 {
let sum: f64 = sum(values);
let size: f64 = len(values) as f64;
sum / size
}
Some of the conversions which can be done through the as
operator
can also be done implicitly at various points in the program, such as
argument passing and assignment to a let
binding with an explicit
type. Implicit conversions are limited to "harmless" conversions that
do not lose information and which have minimal or no risk of
surprising side-effects on the dynamic execution semantics.
Assignment expressions
An assignment expression consists of a pattern followed by an equals
sign (=
) and an expression.
Evaluating an assignment expression either copies or moves its right-hand operand to its left-hand operand.
# let mut x = 0;
# let y = 0;
x = y;
Compound assignment expressions
The +
, -
, *
, /
, %
, &
, |
, ^
, <<
, and >>
operators may be
composed with the =
operator. The expression lval OP= val
is equivalent to
lval = lval OP val
. For example, x = x + 1
may be written as x += 1
.
Any such expression always has the unit
type.
Operator precedence
The precedence of Rust binary operators is ordered as follows, going from strong to weak:
as :
* / %
+ -
<< >>
&
^
|
== != < > <= >=
&&
||
.. ...
<-
=
Operators at the same precedence level are evaluated left-to-right. Unary operators have the same precedence level and are stronger than any of the binary operators.