Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix B: Operators and Symbols

Arithmetic operators

OperatorMeaningTrait
a + bAddAdd
a - bSubtractSub
a * bMultiplyMul
a / bDivideDiv
a % bRemainderRem

All arithmetic operators lower to trait-method calls after type checking. Writing a + b where A does not implement Add is a type error that names the missing trait, not the operator.

Integer arithmetic uses trap-on-overflow semantics in app and lib profiles. Use named methods for explicit control:

MethodBehavior
a.checked_add(b)Option[T]Returns None on overflow
a.saturating_add(b)Clamps to T::MAX / T::MIN
a.wrapping_add(b)Two's-complement wraparound
a.overflowing_add(b)(T, bool)Wraparound + overflow flag

Bitwise operators

OperatorMeaningTrait
a & bBitwise ANDBitAnd
a | bBitwise ORBitOr
a ^ bBitwise XORBitXor
a << bLeft shiftShl
a >> bRight shiftShr

Comparison operators

OperatorMeaningTrait
a == bEqualPartialEq
a != bNot equalPartialEq
a < bLess thanPartialOrd
a <= bLess than or equalPartialOrd
a > bGreater thanPartialOrd
a >= bGreater than or equalPartialOrd

Logical operators

OperatorMeaning
a && bShort-circuit logical AND
a || bShort-circuit logical OR
!aLogical NOT

Assignment operators

OperatorMeaning
a = bAssign
a += bAdd-assign
a -= bSubtract-assign
a *= bMultiply-assign
a /= bDivide-assign
a %= bRemainder-assign
a &= bBitwise-AND-assign
a |= bBitwise-OR-assign
a ^= bBitwise-XOR-assign
a <<= bLeft-shift-assign
a >>= bRight-shift-assign

Range operators

OperatorMeaningExample
a..bHalf-open range [a, b)0..10
a..=bClosed range [a, b]1..=5
a..Range from a to endslice[2..]
..bRange from start to b (exclusive)slice[..4]
..Full rangeslice[..]

Other operators and symbols

SymbolMeaning
?Propagate an Err result early (error shorthand)
a |> fPipe a as the first argument to f
a ?? bNil-coalesce: return a if it is Some, else b
a?.bOptional chaining: access .b only if a is Some
a as TCast a to type T
*Prefix dereference (planned — *r where r: ref T yields T)
_Wildcard pattern or unnamed placeholder
..Struct-update spread in struct literals
->Return-type annotation in function signatures
=>Pattern arm separator in match
::Path separator in qualified names
@Attribute prefix
#[...]Attribute on a declaration

Numeric literal suffixes

Force the type of a literal where inference cannot propagate from a binding annotation.

SuffixType
42i8i8
42i16i16
42i32i32
42i64i64 (default for integer literals)
42u8u8
42u16u16
42u32u32
42u64u64
1.0f32f32
1.0f64f64 (default for float literals)

Unsuffixed integer literals default to i64; unsuffixed float literals default to f64. In a binary expression, an unsuffixed literal may be promoted to the type of its suffixed sibling.

String literal prefixes

PrefixMeaning
"..."Plain string literal
f"..."Interpolated string — {expr} inserts the Display value of expr
f"...{expr:?}..."Interpolated with Debug formatting
b"..."Byte string (future)
r"..."Raw string — no escape processing (reserved, not yet implemented)