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

Expressions

Expression              = ExpressionWithoutBlock
                        | ExpressionWithBlock
                        ;
ExpressionWithBlock     = BlockExpression
                        | IfExpressionWithBlock
                        | LoopExpression
                        ;
ExpressionWithoutBlock  = GroupedExpression
                        | IfExpressionWithoutBlock
                        | LiteralExpression
                        | OperatorExpression
                        | PlaceExpression
                        | FunctionCallExpression
                        | StructExpression
                        | ArrayExpression
                        | BitCastExpression
                        ;
PlaceExpression         = PathExpression
                        | ArrayIndexExpression
                        | FieldAccessExpression
                        ;

An expression is an element of the program which can be evaluated to produce a value.

An expression can have effects (“side effects”) during evaluation.

The meaning of each kind of expression dictates:

  • whether the expression contains sub-expressions (operands)
  • under what circumstances operands are evaluated (including re-evaluation)
  • how the values produced by operands combine to obtain the expressions value

Expressions with blocks

Block expressions, loop expressions and if expressions whose last branch is a block are expressions with blocks. They can only be used as a whole expression, where nothing follows them:

They cannot be used as an operand of any other operator, or as an array item, a struct field value, an array index or an if condition. To use one there, wrap it in parentheses:

global G: u8;

fn main() {
    let a: u8 = ({ yield G; }) + 1; // ok
    let b: u8 = a - ({ yield 3; }); // ok
}
fn main() {
    let a: u8 = { yield 1; } + 1; // error: cannot be used as an operand
    let b: u8 = a - { yield 3; }; // error: cannot be used as an operand
}

Similarly, an assignment whose right-hand side is an expression with a block cannot be followed by another operator: x = { yield 1; } + 1 is an error, and must be written as x = ({ yield 1; }) + 1.

Expression precedence

The order in which expressions are evaluated is governed by a set of conventions.

  • expression types can be grouped and ranked into levels
  • this rank is called the expression precedence
  • levels are ranked from strongest (highest) to weakest (lowest) precedence, where stronger precedence expressions are evaluated before weaker precedence
  • binary operator expressions within the same level are further ordered by the associativity, either from left to right, from right to left, or neither in which case they require parentheses
  • expression precedence can be overridden using parentheses forming a grouped expression
Expression typeAssociativity
GroupedN/A
PathsN/A
Field accessleft to right
Function calls, Array indexN/A
Unary -, !N/A
asleft to right
Binary -, +left to right
<<, >>, <%, >%N/A
&left to right
^left to right
|left to right
==, !=, <, >, <=, >=requires parentheses
&&left to right
||left to right
=, +=, -=, &=, |=, ^=right to left

Place expressions

Place expressions are expressions which represent a location in memory or on a register which can be referenced.

They include: