Operators in Rust
Learn arithmetic, comparison, logical, bitwise, and range operators in Rust with practical Docker-ready examples
Operators are the verbs of a programming language—the symbols that combine values into new ones. Rust gives you the familiar set of arithmetic, comparison, and logical operators, but its strong, static type system shapes how they behave in ways that may surprise newcomers from dynamic languages.
The defining trait of Rust operators is that they are type-disciplined. You cannot add an integer to a floating-point number without an explicit conversion, integer division truncates rather than producing a float, and there is no implicit coercion to paper over mismatches. The compiler checks every operation, so a program that compiles will not silently corrupt a value through an unexpected type promotion.
Rust also reflects its functional heritage: most “statements” are actually expressions that produce a value. An if block, an arithmetic expression, and a range all evaluate to something you can bind or pass along. In this tutorial you will work through arithmetic, comparison, logical, compound-assignment, bitwise, and range operators, and see how operator precedence resolves complex expressions.
Arithmetic, Comparison, Logical, and Bitwise Operators
The example below exercises the core operator families in a single program. Note that integer division (17 / 5) truncates toward zero, while floating-point division keeps the fractional part. Rust deliberately has no ++ or -- operators—use += 1 instead.
Create a file named operators.rs:
| |
A few Rust-specific details worth highlighting:
/on integers truncates.17 / 5is3, not3.4. To get a float, at least one operand must be a float (17.0 / 5.0).- Comparison operators always yield a
bool. There is no “truthy” coercion—if 1is a type error in Rust. &&and||short-circuit. The right-hand side is only evaluated if needed, which matters when it has side effects.{:04b}is a format specifier: print in binary, zero-padded to 4 digits. The<<operator shifts bits left.
String Concatenation
Rust separates the heap-allocated, growable String from the borrowed string slice &str, and this distinction is visible in how concatenation works. The + operator consumes (moves) the left-hand String and borrows the right-hand &str. When you need to keep all the original values, reach for the format! macro instead—it borrows everything and allocates a fresh String.
Create a file named string_ops.rs:
| |
Because greeting + ... moves greeting, you could not use greeting again afterward—the compiler would reject it. With format!, both first and second remain valid, which is why the third println! works. This is the ownership system showing up even in something as routine as joining strings.
Running with Docker
Use the official Rust image to compile and run each example without installing a toolchain locally.
| |
Expected Output
Running operators.rs:
Arithmetic:
17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2
17 / 5 = 3.4
Comparison:
17 == 5 -> false
17 != 5 -> true
17 < 5 -> false
17 >= 5 -> true
Logical:
sunny && warm -> false
sunny || warm -> true
!sunny -> false
Compound assignment: total = 24
Bitwise:
1100 & 1010 = 1000
1100 | 1010 = 1110
1100 ^ 1010 = 0110
1 << 4 = 16
Precedence:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Range: sum of 1..=5 = 15
Running string_ops.rs:
Hello, Rust!
CodeArchaeology
Still usable: Code and Archaeology
==========
Key Concepts
- Integer division truncates.
17 / 5is3; use floating-point operands for a fractional result. The%operator gives the remainder. - No
++or--. Rust has no increment/decrement operators—use compound assignment likecount += 1. - Compound assignment needs
mut. Operators like+=,-=, and*=mutate the binding, so the variable must be declaredlet mut. - Comparisons return
bool, never coerced. Rust has no truthiness; conditions must be genuine booleans. - Logical
&&and||short-circuit, evaluating the right operand only when necessary. - Bitwise operators (
&,|,^,<<,>>) work on integers and pair well with binary format specifiers like{:04b}. - Precedence follows math conventions:
*,/, and%bind tighter than+and-; use parentheses to override. - Ranges are operators too.
1..5is exclusive and1..=5is inclusive; both produce iterators you can.sum(), loop over, or slice with.
Comments
Loading comments...
Leave a Comment