Operators & Expressions

Operators are the symbols that combine values — arithmetic, comparison, logical, and bitwise — and in Rust nearly every construct is an expression that produces a value you can use directly.

Learn Operators & Expressions in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll learn Rust's operators, how integer overflow is handled with wrapping_add and friends, why there's no ++ , and how if and blocks return values.

What You'll Learn in This Lesson

1️⃣ Arithmetic & Overflow

The arithmetic operators + - * / % behave as you'd expect, with one catch: dividing two integers truncates toward zero, while dividing floats keeps the fraction. Remember there is no ++ — increment with += on a mut variable.

Integers have fixed sizes, so a result can be too big to fit. Rust does not silently corrupt data: a debug build panics on overflow. When you actually want a specific behavior, you ask for it by name:

2️⃣ Comparison, Logical & Bitwise

Comparison operators ( ) always produce a bool . The logical operators && and || combine booleans and short-circuit — they stop evaluating as soon as the answer is decided. The bitwise operators & | ^ ! << >> work on the individual bits of integers.

Don't confuse && (logical AND on booleans, short-circuiting) with & (bitwise AND on integer bits). They are different operators for different jobs.

3️⃣ Everything Is an Expression

This is what makes Rust feel different. An if / else is an expression : it evaluates to a value you can bind to a variable. A block {' '} evaluates to its last line when that line has no trailing semicolon. So does match . You rarely need a temporary mutable variable just to compute something conditionally.

Your turn. Fill in the blanks marked ___ , then run it.

Combine arithmetic, the remainder operator, and a comparison to summarize an array. Run it with cargo run and check the output.

📋 Quick Reference — Operators

Practice quiz

What is the result of 17 / 5 in Rust when both are integers?

  • 3.4
  • 3
  • 4
  • 2

Answer: 3. Integer division truncates toward zero, so 17 / 5 is 3.

What does the % operator compute?

  • Percentage
  • Remainder (modulo)
  • Exponent
  • Division

Answer: Remainder (modulo). % gives the remainder, e.g. 17 % 5 is 2.

How do you increment a mutable variable in Rust?

  • count++
  • ++count
  • count += 1
  • count =+ 1

Answer: count += 1. Rust has no ++; use the compound operator count += 1.

What does 255u8.wrapping_add(1) return?

  • 256
  • 0
  • None
  • a panic

Answer: 0. wrapping_add wraps a u8 around from 255 back to 0.

What does 255u8.checked_add(1) return?

  • Some(0)
  • 0
  • None
  • 256

Answer: None. checked_add returns None when the operation would overflow.

What does 255u8.saturating_add(50) return?

  • 305
  • 49
  • 255
  • None

Answer: 255. saturating_add clamps to the type maximum, which is 255 for u8.

What is 0b1100 & 0b1010 (bitwise AND)?

  • 8
  • 14
  • 6
  • 12

Answer: 8. Bitwise AND keeps only bits set in both, giving 0b1000 which is 8.

Which operators short-circuit on booleans?

  • & and |
  • && and ||
  • ^ and !
  • << and >>

Answer: && and ||. Logical && and || stop evaluating as soon as the answer is known.

What does let g = if x >= 60 { "pass" } else { "fail" }; show?

  • if is a statement
  • if is an expression that yields a value
  • This is a syntax error
  • g is always ()

Answer: if is an expression that yields a value. In Rust if is an expression, so it can be bound directly to a variable.

Why does Rust reject 5 + 2.0 directly?

  • Floats are not allowed
  • No implicit numeric coercion; operands must share a type
  • + only works on integers
  • 2.0 is not a number

Answer: No implicit numeric coercion; operands must share a type. Rust has no implicit numeric coercion; cast explicitly, e.g. (5 as f64) + 2.0.