Rust Reference ยท Operators and Expressions
Rust arithmetic operators
Reference for Rust arithmetic operators +, -, *, / and %: integer and float behavior, overflow panics, division by zero, remainder signs and checked variants.
Rust has five binary arithmetic operators: +, -, *, / and % (remainder), plus unary - for negation. They work on numbers of the same type. There is no implicit conversion, so 1_i32 + 1_i64 is an error and you convert with as or From. Each operator is backed by a trait in std::ops (Add, Sub, Mul, Div, Rem, Neg), which is how your own types can support them.
Integers behave strictly: overflow is a bug. In debug builds an overflowing operation panics; in release builds with default settings it wraps around. When you need a defined behavior, use the checked_, wrapping_, saturating_ or overflowing_ method families.
let total = price * quantity + shipping; let half = 7 / 2; // 3 let rest = 7 % 2; // 1
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
a + b a - b a * b a / b a % b -a a += b a -= b a *= b a /= b a %= b
| Operator | Trait | Description |
|---|---|---|
+ |
Add |
Addition; also concatenates String + &str. |
- |
Sub |
Subtraction; unary - is Neg (signed numbers only). |
* |
Mul |
Multiplication. |
/ |
Div |
Integer division truncates toward zero; floats divide exactly as IEEE 754. |
% |
Rem |
Remainder; the result takes the sign of the left operand. |
Panics (integers): / or % by zero always panics (in every build). i32::MIN / -1 and i32::MIN % -1 overflow and panic. +, -, * and unary - panic on overflow when overflow checks are on (the debug default). Constant expressions that must overflow are rejected at compile time by the arithmetic_overflow lint.
Floats: never panic. Division by zero gives infinity or NaN; the result of % takes the sign of the dividend.
Usage notes
- No exponent operator. Use
powfor integers (2_i32.pow(10)),powiorpowffor floats.^is bitwise XOR, not power. - Same type on both sides. Mixing
i32andf64oru8andu32is a compile error (E0277). Widen withu32::from(x)when lossless, oraswhen truncation is acceptable. - Precedence:
*,/,%bind tighter than+,-; all are left-associative. See precedence. - References work:
&a + &b,a + &band&a + bare all implemented for the primitives, which keeps iterator code likeiter().map(|x| x * 2)tidy. - Rounding semantics:
/truncates toward zero,%follows it, soa == (a / b) * b + a % b. For floor semantics and non-negative remainders usediv_euclidandrem_euclid. - Release behavior: wrapping on overflow in default release builds is not guaranteed by the language; set
overflow-checks = truein the profile to keep checks, or use the explicit method families. - Operator overloading: implement
Add<Rhs>and friends for your own types;Outputneed not beSelf. - Compound assignment (
+=and others) uses the matchingAddAssignstyle trait and avoids a temporary for types likeStringandVec.