8. Statements

Syntax

Statement ::=
    ExpressionStatement
  | Item
  | LetStatement
  | TerminatedMacroInvocation
  | ;

Legality Rules

8:1 An expression statement is an expression whose result is ignored.

8:2 An item statement is a statement that is expressed as an item.

8:3 An empty statement is a statement expressed as character 0x3B (semicolon).

8:4 A macro statement is a statement expressed as a terminated macro invocation.

Dynamic Semantics

8:5 Execution is the process by which a statement achieves its runtime effects.

8:6 The execution of an empty statement has no effect.

8.1. Let Statements

Syntax

LetStatement ::=
    OuterAttributeOrDoc* let PatternWithoutAlternation TypeAscription? LetInitializer? ;

LetInitializer ::=
    = Expression (else BlockExpression)?

Legality Rules

8.1:1 A let statement is a statement that introduces new bindings produced by its pattern-without-alternation that are optionally initialized to a value.

8.1:2 A let initializer is a construct that provides the value of the bindings of the let statement using an expression, or alternatively executes a block expression.

8.1:3 If a let statement lacks a block expression, then the pattern of the let statement shall be an irrefutable pattern.

8.1:4 If a let statement has a block expression, then the Expression of the LetInitializer shall not be a LazyBooleanExpression or end with token }.

8.1:5 The expected type of the pattern of the let statement is determined as follows:

8.1:9 The type of a binding introduced by a let statement is determined as follows:

8.1:12 The type of the block expression of a let statement shall be the never type.

8.1:13 The value of a binding introduced by a let statement is determined as follows:

Dynamic Semantics

8.1:16 The execution of a let statement with a let initializer proceeds as follows:

  1. 8.1:17 The expression of the let initializer is evaluated.

  2. 8.1:18 If the value of the expression is matched successfully against the pattern of the let statement, then the value is assigned to each binding introduced by the let statement.

  3. 8.1:19 Otherwise the block expression of the let initializer is evaluated.

Examples

let local = 0;
let local: u32;
let (a, b) = (0, 0);
let Some(value) = vector.pop() else {
    panic!();
};

8.2. Expression Statements

Syntax

ExpressionStatement ::=
    ExpressionWithBlock ;?
  | ExpressionWithoutBlock ;

Legality Rules

8.2:1 An expression statement is an expression whose result is ignored.

8.2:2 The expected type of an expression statement without character 0x3B (semicolon) is the unit type.

Dynamic Semantics

8.2:3 The execution of an expression statement proceeds as follows:

  1. 8.2:4 The operand is evaluated.

  2. 8.2:5 The value of the operand is dropped.

Examples

let mut values = vec![1, 2, 3];

8.2:6 The following expression statement ignores the result from pop().

values.pop();

8.2:7 The following expression statement does not require a semicolon.

if values.is_empty() {
    values.push(42);
}
else {
    values.remove(0);
}

8.2:8 The following expression statement is not an index expression.

[42];