Beginner Fundamentals

Operators

Operators let you compute values and make decisions. Rust groups them into arithmetic, comparison, and logical operators.

Arithmetic operators

fn main() {
    let a = 10;
    let b = 3;
    println!("{}", a + b); // 13
    println!("{}", a - b); // 7
    println!("{}", a * b); // 30
    println!("{}", a / b); // 3 (integer division)
    println!("{}", a % b); // 1 (remainder)
}

Comparison operators

These return a bool.

fn main() {
    let x = 5;
    let y = 8;
    println!("{}", x == y); // false
    println!("{}", x != y); // true
    println!("{}", x < y);  // true
    println!("{}", x >= y); // false
}

Logical operators

Combine boolean values with && (and), || (or), and ! (not).

fn main() {
    let sunny = true;
    let warm = false;
    println!("{}", sunny && warm); // false
    println!("{}", sunny || warm); // true
    println!("{}", !sunny);        // false
}

The && and || operators short-circuit: they stop evaluating as soon as the result is known.