Skip to content
Tutorials Center
My learning

Topics

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.

Rust
let total = price * quantity + shipping;
let half = 7 / 2;     // 3
let rest = 7 % 2;     // 1

Try it

Rust Integer and float arithmetic

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.

Rust Handling overflow explicitly

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

Rust
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 pow for integers (2_i32.pow(10)), powi or powf for floats. ^ is bitwise XOR, not power.
  • Same type on both sides. Mixing i32 and f64 or u8 and u32 is a compile error (E0277). Widen with u32::from(x) when lossless, or as when truncation is acceptable.
  • Precedence: *, /, % bind tighter than +, -; all are left-associative. See precedence.
  • References work: &a + &b, a + &b and &a + b are all implemented for the primitives, which keeps iterator code like iter().map(|x| x * 2) tidy.
  • Rounding semantics: / truncates toward zero, % follows it, so a == (a / b) * b + a % b. For floor semantics and non-negative remainders use div_euclid and rem_euclid.
  • Release behavior: wrapping on overflow in default release builds is not guaranteed by the language; set overflow-checks = true in the profile to keep checks, or use the explicit method families.
  • Operator overloading: implement Add<Rhs> and friends for your own types; Output need not be Self.
  • Compound assignment (+= and others) uses the matching AddAssign style trait and avoids a temporary for types like String and Vec.