Rust Operators
Operators are the symbols you use to do math, compare values, and combine conditions. Most of these will look familiar if you’ve used any C-like language.
Arithmetic operators
Section titled “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, drops the remainder) println!("{}", a % b); // 1 (remainder)}Comparison operators
Section titled “Comparison operators”These compare two values and give back a bool:
fn main() { let a = 5; let b = 8;
println!("{}", a == b); // false println!("{}", a != b); // true println!("{}", a < b); // true println!("{}", a > b); // false println!("{}", a <= b); // true println!("{}", a >= b); // false}You’ll use these constantly inside if conditions, which we cover on the If…Else page.
Logical operators
Section titled “Logical operators”Logical operators combine or invert boolean values:
fn main() { let is_logged_in = true; let is_admin = false;
println!("{}", is_logged_in && is_admin); // false — both must be true println!("{}", is_logged_in || is_admin); // true — at least one is true println!("{}", !is_admin); // true — flips false to true}&&— and: true only if both sides are true.||— or: true if at least one side is true.!— not: flips true to false and vice versa.
Assignment operators
Section titled “Assignment operators”Beyond the plain =, Rust has shorthand operators that update a variable based on its current value:
fn main() { let mut score = 10;
score += 5; // same as: score = score + 5 score -= 2; // score = score - 2 score *= 3; // score = score * 3 score /= 2; // score = score / 2
println!("{score}");}Remember the variable needs mut for any of these to compile — you’re changing its value.
Order of operations (precedence)
Section titled “Order of operations (precedence)”When an expression has several operators, Rust doesn’t just go left to right. Like in school math, *, / and % happen before + and -. Operators at the same level run left to right. Parentheses always go first, so use them whenever you want a different order.
fn main() { let a = 2 + 3 * 4; // * first: 2 + 12 let b = (2 + 3) * 4; // parentheses first: 5 * 4 let c = 10 - 4 - 3; // same level, left to right: (10 - 4) - 3 let d = 17 % 5 * 2; // same level, left to right: (17 % 5) * 2 let ok = 1 + 1 == 2 && 3 > 2; // math, then comparisons, then &&
println!("{a} {b} {c} {d} {ok}");}Output:
14 20 3 4 trueThe full order, from first to last, is roughly: arithmetic (* / %, then + -), then comparisons (== != < > <= >=), then &&, then ||, and finally the assignment operators (= += -= and friends). That’s why 1 + 1 == 2 && 3 > 2 works without any parentheses. Still, if you have to stop and think about the order, add parentheses. They cost nothing and make the code easier to read.
Practice
Section titled “Practice”1. Minutes and seconds
Section titled “1. Minutes and seconds”A video is 125 seconds long. Store that in a variable, then print how many whole minutes and leftover seconds it is, like 2 minutes and 5 seconds. Hint: integer division / gives the whole minutes, and % gives what’s left over.
Show solution
fn main() { let total_seconds = 125;
let minutes = total_seconds / 60; // 2 (the remainder is dropped) let seconds = total_seconds % 60; // 5 (the remainder)
println!("{minutes} minutes and {seconds} seconds");}Output:
2 minutes and 5 seconds2. Keeping score
Section titled “2. Keeping score”Start a game score at 10. Add 25, take away 5, then double it, using the shorthand assignment operators. Print the score, whether it is at least 50, and whether it is at least 50 and even. Hint: a number is even when score % 2 == 0.
Show solution
fn main() { let mut score = 10;
score += 25; // 35 score -= 5; // 30 score *= 2; // 60
println!("score: {score}"); println!("passed the level? {}", score >= 50); println!("passed and even? {}", score >= 50 && score % 2 == 0);}Output:
score: 60passed the level? truepassed and even? true- Arithmetic:
+ - * / %. Integer division truncates — no decimals. - Comparison:
== != < > <= >=, all return abool. - Logical:
&&(and),||(or),!(not). - Assignment shorthand:
+= -= *= /=update amutvariable in place. There is no++. * / %run before+ -, and math runs before comparisons and&&/||. Use parentheses when in doubt.
Quick check
Section titled “Quick check”Quick check
1. What does 10 / 3 evaluate to when both sides are integers?
2. What type do comparison operators like == and < always return?
3. What's required for score += 5; to compile?
Score: 0 / 3
Read more
Section titled “Read more”- B - Operators and Symbols — The Rust Book: a one-page table of every operator in Rust and what it does.
- Literals and operators — Rust by Example: short, runnable examples of arithmetic, logic and bitwise operators.
- Expression precedence — The Rust Reference: the exact order Rust evaluates operators in, from first to last.
- Operator expressions — The Rust Reference: the precise rules for each operator, including what happens on integer overflow.