Operators & Expressions
Fe provides a comprehensive set of operators for arithmetic, comparison, logic, and bit manipulation, along with various expression types for building complex computations.
Arithmetic Operators
Section titled “Arithmetic Operators”Arithmetic operators work on numeric types.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulo (remainder) | a % b |
** | Exponentiation | a ** b |
let sum: u256 = 10 + 5 // 15let diff: u256 = 10 - 5 // 5let product: u256 = 10 * 5 // 50let quotient: u256 = 10 / 3 // 3 (integer division)let remainder: u256 = 10 % 3 // 1let power: u256 = 2 ** 8 // 256Integer Division
Section titled “Integer Division”Division between integers performs integer division, truncating toward zero:
let result: i256 = 7 / 2 // 3, not 3.5let neg: i256 = -7 / 2 // -3Comparison Operators
Section titled “Comparison Operators”Comparison operators return bool values.
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
< | Less than | a < b |
<= | Less than or equal | a <= b |
> | Greater than | a > b |
>= | Greater than or equal | a >= b |
let x: u256 = 5let y: u256 = 10
let is_equal = x == y // falselet is_not_equal = x != y // truelet is_less = x < y // truelet is_less_eq = x <= 5 // truelet is_greater = x > y // falselet is_greater_eq = y >= 10 // trueLogical Operators
Section titled “Logical Operators”Logical operators work on bool values.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
let a = truelet b = false
let and_result = a && b // falselet or_result = a || b // truelet not_result = !a // falseShort-Circuit Evaluation
Section titled “Short-Circuit Evaluation”Logical operators use short-circuit evaluation:
&&stops evaluating if the left operand isfalse||stops evaluating if the left operand istrue
// If is_valid is false, expensive_check() won't be calledif is_valid && expensive_check() { // ...}
// If has_default is true, compute_value() won't be calledlet value = has_default || compute_value()Bitwise Operators
Section titled “Bitwise Operators”Bitwise operators manipulate individual bits of integer values.
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << n |
>> | Right shift | a >> n |
let a: u8 = 0b1100let b: u8 = 0b1010
let and_bits: u8 = a & b // 0b1000 (8)let or_bits: u8 = a | b // 0b1110 (14)let xor_bits: u8 = a ^ b // 0b0110 (6)let not_bits: u8 = ~a // inverts all bits
let shifted_left: u8 = a << 2 // 0b110000 (48)let shifted_right: u8 = a >> 2 // 0b0011 (3)Common Bitwise Patterns
Section titled “Common Bitwise Patterns”const FLAG: u8 = 0b0010
// Check if a bit is setlet has_flag = (value & FLAG) != 0
// Set a bitlet with_flag = value | FLAG
// Clear a bitlet without_flag = value & ~FLAG
// Toggle a bitlet toggled = value ^ FLAGUnary Operators
Section titled “Unary Operators”Unary operators take a single operand.
| Operator | Description | Example |
|---|---|---|
+ | Unary plus (identity) | +x |
- | Negation | -x |
! | Logical NOT | !flag |
~ | Bitwise NOT | ~bits |
let x: i256 = 42let negative: i256 = -x // -42let positive: i256 = +x // 42 (no change)
let flag = truelet inverted = !flag // false
let bits: u8 = 0b11110000let flipped: u8 = ~bits // 0b00001111Assignment Operators
Section titled “Assignment Operators”Simple Assignment
Section titled “Simple Assignment”Use = to assign a value to a mutable variable:
let mut x: u256 = 10x = 20 // x is now 20Compound Assignment
Section titled “Compound Assignment”Compound assignment operators combine an operation with assignment:
| Operator | Equivalent to | Description |
|---|---|---|
+= | x = x + y | Add and assign |
-= | x = x - y | Subtract and assign |
*= | x = x * y | Multiply and assign |
/= | x = x / y | Divide and assign |
%= | x = x % y | Modulo and assign |
&= | x = x & y | Bitwise AND and assign |
|= | x = x | y | Bitwise OR and assign |
^= | x = x ^ y | Bitwise XOR and assign |
<<= | x = x << y | Left shift and assign |
>>= | x = x >> y | Right shift and assign |
let mut count: u256 = 10count += 5 // count is now 15count -= 3 // count is now 12count *= 2 // count is now 24
let mut flags: u8 = 0b0000flags |= 0b0001 // Set bit 0flags |= 0b0100 // Set bit 2// flags is now 0b0101Expression Types
Section titled “Expression Types”Fe is an expression-oriented language. Most constructs produce values.
Block Expressions
Section titled “Block Expressions”A block { } is an expression that evaluates to its last expression:
let result: u256 = { let a: u256 = 10 let b: u256 = 20 a + b // no semicolon - this is the block's value}// result is 30Function Calls
Section titled “Function Calls”Call functions with parentheses:
let value = compute(10, 20)let result = process(data, flag: true) // with labeled argumentMethod Calls
Section titled “Method Calls”Call methods on values using dot notation:
let doubled = counter.multiply(by: 2)let incremented = counter.add(amount: 10)Field Access
Section titled “Field Access”Access struct fields with dot notation:
let x = point.xlet name = user.profile.nameIndexing
Section titled “Indexing”Access array and tuple elements by index:
let first = arr[0]let third = arr[2]
let x = tuple.0let y = tuple.1Tuple Expressions
Section titled “Tuple Expressions”Create tuples with parentheses:
let point: (u256, u256) = (10, 20)let triple: (u256, bool, String<5>) = (1, true, "hello")let unit: () = () // empty tuple (unit type)Array Expressions
Section titled “Array Expressions”Create arrays with brackets:
let numbers: [u256; 5] = [1, 2, 3, 4, 5]let flags: [bool; 3] = [true, false, true]
// Repeat syntax: [value; count]let zeros: [u256; 10] = [0; 10] // array of 10 zerosIf Expressions
Section titled “If Expressions”if is an expression that returns a value:
let max = if a > b { a } else { b }
let description = if count == 0 { "none"} else if count == 1 { "one"} else { "many"}Match Expressions
Section titled “Match Expressions”match is an expression for pattern matching:
let result = match value { 0 => "zero" 1 => "one" _ => "other"}Parenthesized Expressions
Section titled “Parenthesized Expressions”Use parentheses to control evaluation order:
let result: u256 = (a + b) * clet complex: u256 = ((x + y) * z) / wOperator Precedence
Section titled “Operator Precedence”Operators are evaluated according to their precedence. Higher precedence operators bind tighter.
| Precedence | Operators | Associativity |
|---|---|---|
| Highest | (), ., [] | Left to right |
Unary +, -, !, ~ | Right to left | |
** | Right to left | |
*, /, % | Left to right | |
+, - | Left to right | |
<<, >> | Left to right | |
& | Left to right | |
^ | Left to right | |
| | Left to right | |
==, !=, <, <=, >, >= | Left to right | |
&& | Left to right | |
|| | Left to right | |
| Lowest | =, +=, -=, etc. | Right to left |
When in doubt, use parentheses to make your intent clear:
// These are equivalent, but the second is clearerlet result: u256 = a + b * clet result: u256 = a + (b * c)Summary
Section titled “Summary”| Category | Operators |
|---|---|
| Arithmetic | +, -, *, /, %, ** |
| Comparison | ==, !=, <, <=, >, >= |
| Logical | &&, ||, ! |
| Bitwise | &, |, ^, ~, <<, >> |
| Assignment | =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= |