Chapter 10: Rust Operators

Rust Operators

Operators are the “verbs” of programming — they tell the computer what to do with values (add, compare, check truth, etc.). Rust’s operators are very similar to C/C++/Java/Python, but with some Rust-specific twists (like safe overflow behavior in debug mode, the ? operator for errors, and powerful overloading via traits).

Today I’ll explain everything like your patient teacher: slowly, category by category, with lots of copy-paste examples you can run in your project, real Hyderabad-flavored analogies, precedence rules (very important!), and tips on what surprises beginners.

1. Categories of Operators in Rust

Rust groups operators into these families:

  • Arithmetic (+ – * / %)
  • Bitwise (& | ^ ~ << >>)
  • Comparison (== != > < >= <=)
  • Logical (&& || !)
  • Assignment (= += -= *= /= %= &= |= ^= <<= >>=)
  • Compound assignment (like above)
  • Unary (- ! * & &mut)
  • Borrow / dereference (* &)
  • Range (.. ..=)
  • Error propagation (?)
  • Type cast (as)
  • Others (indexing [], field ., method call, path ::)

Most are overloadable via traits (std::ops::*) — you can make your own types work with +, *, etc.

2. Arithmetic Operators (Most Common)

Work on numbers (i32, f64, etc.)

Rust

Important notes:

  • Integer division truncates toward zero (15 / 4 = 3)
  • In debug mode: overflow panics (let x = 255u8 + 1; → panic!)
  • In release mode: wraps around (255u8 + 1 = 0)
  • Use checked_add, wrapping_add, saturating_add for control

3. Comparison & Logical Operators

Rust
  • && and || are short-circuit (right side skipped if not needed)
  • ! = logical NOT

4. Bitwise Operators (Low-level magic)

Used for flags, masks, binary ops.

Rust

Great for bit flags (permissions, colors, etc.).

5. Assignment & Compound Assignment

Rust

All arithmetic + bitwise have compound versions (+=, &=, etc.).

6. Unary Operators

Rust
  • – unary minus
  • ! logical/bitwise NOT
  • & immutable borrow
  • &mut mutable borrow
  • * dereference (for references/smart pointers)

7. Special Rust Operators (These Make Rust Unique)

A. Range operators .. and ..=

Rust

B. The famous ? operator (error propagation)

Rust

? = “try this, if error bubble it up”

C. as for type casting

Rust

8. Operator Precedence (Very Important — Order of Operations)

Rust follows a clear precedence table (from highest to lowest — official reference style):

  1. Paths, method calls, field access .
  2. Function calls, indexing []
  3. ?
  4. Unary – ! * & &mut
  5. as (casting)
  6. * / % (multiply, divide, remainder)
  7. + – (add, subtract)
  8. << >> (shifts)
  9. & (bitwise AND)
  10. ^ (bitwise XOR)
  11. (bitwise OR)
  12. == != < > <= >= (comparisons)
  13. && (logical AND)
  14. || (logical OR)
  15. Assignment = += -= etc.

Rule: Higher precedence first. Same level → left-to-right (except assignment right-to-left).

Example:

Rust

Always use parentheses () when in doubt — makes code clearer!

Quick Practice Project

Add this to your main.rs:

Rust

Run → experiment! Change numbers, add parentheses, see what breaks.

Teacher Summary

Rust operators feel familiar but safer:

  • Arithmetic like school math (but watch integer division & overflow)
  • Logical & comparison same as most languages
  • Bitwise for power users
  • ? = Rust’s gift for clean error handling
  • Precedence same as C-family → parentheses save headaches

Next ready?

  • Control flow (if, match, loops)?
  • Functions + how operators work in them?
  • Or strings & formatting (where + and format! use operators)?

Just tell me — class continues! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *