HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RustVariablesData TypesOperatorsStringsVectors (Vec)Conditions (if / match)Loops (for / while)FunctionsOwnership & Borrowing
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Vector & HashMap
🔁 Condition & Loop
🧩 Function
🔑 Ownership & Borrowing
Rust›Operators

Operators

Math (+ - * / %), comparison (== < >), and logic (&& || !).

Simple enough for a 5-year-old to understand

Math

Example code
1
fn main() {
2
println!("{}", 7 % 2);
Remainder: 1
CodeRemainder: 1
3
println!("{}", 7 / 2);
Integer division: 3
CodeInteger division: 3
4
println!("{}", 2i32.pow(10));
Exponent: 1024
CodeExponent: 1024
5
}
Code
💡 Hint: There is no ** in Rust; for powers, a method like 2i32.pow(10) is used.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of 23 divided by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Rust's operators are similar to the C family, but with a few important differences. There is no ternary operator; instead, if is an EXPRESSION and produces a value, so it can be used on the right side of an assignment.

There are also no increment and decrement operators. Compound assignment is used to increment a variable. This is because these operators produce undefined behavior in C when used within expressions and hurt readability.

In Rust, comparison and arithmetic operators are defined through interfaces called traits. This allows you to define addition or comparison behavior for your own types; there are no special cases at the language level.

What you will learn

  • Arithmetic and comparison operators
  • That if is an expression and there is no ternary operator
  • Why the increment operator does not exist
  • Short-circuit evaluation

When to use?

The if expression is used when choosing between two values; it is readable as long as it stays on a single line and replaces the ternary operator.

When dividing, a check for division by zero is necessary: integer division by zero panics the program, while floating-point division produces infinity or NaN.

Common mistakes

Wrong

fn main() {
    let mut i = 5;
    i++;
    println!("{}", i);
}

There is NO increment operator in Rust; the compiler gives the error "Rust has no postfix increment operator" and suggests using compound assignment. This omission is intentional: using the operator inside expressions is a source of undefined behavior in C.

Correct

fn main() {
    let mut i = 5;
    i += 1;
    println!("{}", i);
}

Wrong

fn main() {
    let age = 20;
    let status = age >= 18 ? "adult" : "child";
    println!("{}", status);
}

Rust does not have a ternary operator, and the question mark has a completely different meaning — it is the error propagation operator. Instead, if is used as an expression and produces a value; the types of the values in the branches must be the same.

Correct

fn main() {
    let age = 20;
    let status = if age >= 18 { "adult" } else { "child" };
    println!("{}", status);
}

Wrong

fn main() {
    let a = 5;
    let b = 10;
    if a > 0 & b > 0 {
        println!("Both are positive");
    }
}

The single ampersand works bitwise; two are needed for logical and. In Rust, this example compiles on boolean values and returns the correct result, but short-circuit evaluation is lost, meaning the right-hand condition is always evaluated.

Correct

fn main() {
    let a = 5;
    let b = 10;
    if a > 0 && b > 0 {
        println!("Both are positive");
    }
}

Frequently asked questions

How can if be both a statement and an expression?

In Rust, when the last expression of a block is written without a semicolon, it becomes the value of the block. Since if blocks have values, if also produces a value. The types of the branches must be the same, otherwise the compiler throws an error.

What does division by zero do?

In integer division, the program panics and prints the message "attempt to divide by zero". In floating-point division, it does not panic, producing infinity or NaN instead. When the divisor can be zero in integers, using checked_div is the correct approach.

How does the modulo operation behave with negative numbers?

The sign of the result is the same as the sign of the left operand, so minus seven modulo three results in minus one. In Python, the same operation gives two. If you are calculating a circular index, the rem_euclid method always produces a positive result and is the right choice.

Summary

There are no increment and decrement operators; compound assignment is used instead.

There is no ternary operator; if is an expression and produces a value.

Logical and bitwise operators are different.

Integer division by zero panics the program.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code