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

Alloc Slice Expression

An alloc slice expression allocates storage for a specified number of values and yields a slice to them.

An alloc slice expression has the slice type whose element type is the initializer function's return type.

Process

At runtime:

  1. the first expression is evaluated to determine the number of values to allocate;
  2. enough space for that number of values of the initializer function's return type is allocated, see region allocation;
  3. the second expression (the initializer function) is evaluated once for each element of the allocation, in order;
  4. if the initializer function accepts a u64 parameter, the zero-based index of the element is provided as its argument;
  5. each resulting value is stored in its corresponding element of the allocated space; and
  6. a slice to the allocated storage is returned.

Assertions

  1. The first expression has type u64, and its expected type is that type.
  2. The second expression has function type function(): T or function(u64): T for some type T.
  3. The initializer function's return type is runtime representable.

Syntax

alloc_slice_expression = alloc, left_bracket, expression, right_bracket, unary_expression ;