Operators

Rust's operators look familiar from any C-family language, but two things are distinctly Rust: there's no implicit type coercion anywhere, and integer overflow is actually checked for you in debug builds.

Arithmetic and comparison

Rust src/main.rs
fn main() {
    let a = 17;
    let b = 5;

    println!("{} {} {} {} {}", a + b, a - b, a * b, a / b, a % b);
    println!("{}", a > b);
}
Output
22 12 85 3 2
true

a / b is 17 / 5 using integer types, so it truncates to 3 — same integer-division rule as C. Comparisons (>, <, ==, !=) work as you'd expect and always produce a bool.

No implicit coercion — you must cast with as

Rust src/main.rs
fn main() {
    let total: i32 = 17;
    let count: i32 = 5;
    let average = total as f64 / count as f64;
    println!("{:.2}", average);
}
Output
3.40

Unlike C, Rust never silently converts between numeric types for you — mixing an i32 and an f64 in one expression is a compile error, full stop. as performs an explicit cast, and here it has to appear on both operands before the division happens as floating-point math.

Overflow is checked — in debug builds

Rust src/main.rs
fn main() {
    let max: u8 = 255;
    let over = max + 1;
    println!("{}", over);
}
Output (cargo run, debug build)
thread 'main' panicked at src/main.rs:3:17:
attempt to add with overflow
Note: a u8 holds 0–255, so 255 + 1 overflows. In a debug build (plain cargo run), Rust panics immediately rather than letting the value silently wrap to 0. In a --release build, that same check is compiled out for speed, and the value does wrap around to 0 — meaning the exact same source code can behave differently between debug and release. If you actually want wrapping on purpose, call max.wrapping_add(1) instead, which makes the intent explicit either way.