Expressions use operators, methods and functions calls to calculate new values using existing values. Although the arithmetic and logical expressions demonstrated below primarily use numbers for examples, similar looking expressions may be formulated that use values of other types.
Integer and floating point numbers may be calculated using arithmetic operators:
- Subtract (or negate when used as a prefix operator)
- Modulo (remainder)
-12 + 14 // 2 6 / 5 // 1 -4 % 3 // -1 (where -4/3 --> -1, truncating towards zero) 2*2 + 2*3 // 10 (standard algebraic operator precedence) 2 * (4 + 1) // 10 (parentheses override operator precedence)
The arithmetic assignment operators (e.g., '+=', '-=', '*=', '/=') offer a convenient way to modify a variable's current value:
counter = 3 counter += 1 // 4. Equivalent to: counter = counter + 1
Additional numeric calculations can be performed using built-in methods. The name of the desired method follows the value, separated by a period ('.'):
(-1).abs // 1 (the absolute value, making a number positive) (pi/2).sin // 1.0 (trigonometric sine function)
Sometimes, the method needs additional values. These are specified within parentheses following the method's name.
4.max(5) // 5 (returns the larger of the two numbers)
Methods are covered more thoroughly in a later chapter. For now, several characteristics are worth mentioning:
- The logic used for a method depends on the type of the value it is applied to (the value it follows). Thus, the method named 'max' applied to an integer is actually a different method than a method with the same name applied to a floating point number. Every type establishes the names of the methods it supports. An error results when trying to use a method not implemented by the value's type.
- The arithmetic operators introduced earlier (e.g., + for add)
are implemented using number type methods:
// The method name equivalent for the + operator is `+` // The method name is enclosed in backticks since it includes punctuation 3+4 // is actually: 3.`+`(4)
Since the arithmetic operators are simply methods, they are also accessible to non-numeric types:
"a"+"b" // "ab" (concatenation results when two strings are added)
- The method name for the negative operator is neg. This distinguishes it from `-`, the subtraction method.
Numbers are automatically converted whenever copied from one number type to another.
imm six i32 = 6u8 // Converts 8-bit unsigned integer to 32-bit signed integer imm sixf f64 = six // Converts 32-bit signed integer to 64-bit floating point number
If the receiving number type is smaller, numeric information may be lost.
The type of a number that results from an arithmetic operation is that of the left-hand number. If necessary, the right hand number is converted prior to the operation.
imm r = 5.0 + 2 // 7.0f32
The logical operators operate on true/false values:
true | (false & true) // true true ^ ~true // true
These bitwise operators may also be used on unsigned integers, as they are viewed as a collection of true/false bits.
0xE0u | 0xB8u // 0xF8u
The shifting of an unsigned integer's bit is accomplished using the shl and shr methods:
0x0Cu.shl(1) // 0x18, after shifting bits left once
Logical assignment operators
These work in a similar manner to the arithmetic assignment operators:
mut flags = 0xC0u flags |= 0x01u // 0xC1u
Other Expression Operators
There are many more expression operators whose description may be found in the chapters relevant to their common use:
- Match and conditional operators
- Collection (this block) operators
- Range constructor (`..`)
- New prefix operator (`+`)