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

Primary Expressions

Primary expressions are the base of the expression grammar; all other expression forms are constructed from them.

Integer Literals

Integer literals are non-negative decimal or hexadecimal values. Hexadecimal literals use the 0x prefix. Underscores may separate digit groups.

A surrounding rule which requires a specific type of an expression gives that type as the expression's expected type. Each expression form's page states any expected type it gives to its sub-expressions.

An integer type admits a literal if its range contains the literal's value. A floating-point type admits an integer literal if it exactly represents its value.

The literal's type is determined as follows:

  1. If the literal has an expected type which admits it, it has that type.
  2. If the literal is immediately (possibly through parentheses) the operand of a negation, it is typed by its negated value. The negated expression has the negation's expected type if one exists which admits the negated value; otherwise, it has the smallest signed integer type which admits it.
  3. If the literal is an operand of an arithmetic or comparison expression whose other operand is a non-literal numeric value, it has the smallest type in that operand's family which admits it. For signed integers, the types are i8, i16, i32, i64; for unsigned integers, the types are u8, u16, u32, u64; for floating-point values, the literal has the other operand's type when that type admits it.
  4. Otherwise, the literal has type i64.

A compile-time error occurs if no applicable type admits the literal's value.

Rule 2 allows the minimum value of a signed type to be written: -2147483648 is admitted by i32, and negating i32's minimum wraps to the minimum. Rule 1 also applies through the negation's expected type, so -5 is admitted when its expected type is i64.

Float Literals

Float literals are non-negative decimal values consisting of a fraction with an optional exponent. Underscores may separate digit groups.

A float literal has type f64, or f32 when f32 is expected. A float literal that is an operand of an arithmetic or comparison expression whose other operand is a non-literal f32 value has type f32 when f32 admits it. When an expected type exists and is neither f32 nor f64, a compile-time error is produced.

Boolean Literals

The keywords true and false denote the corresponding values of bool.

String Literals

String literals are byte sequences enclosed in quotation marks. Escape sequences represent individual bytes:

  • \n — line feed
  • \r — carriage return
  • \t — character tabulation
  • \\ — reverse solidus
  • \" — quotation mark
  • \xHH — byte with hexadecimal value HH

A string literal has type []u8 and denotes a slice over statically allocated storage; no allocation is required.

Whether two string literals with identical contents denote the same static storage is implementation-defined; consequently, equality of two string-literal slices is implementation-defined.

Character Literals

Character literals are single bytes enclosed in apostrophes. Their escape sequences are the same as those of string literals, with \' denoting an apostrophe:

  • \n — line feed
  • \r — carriage return
  • \t — character tabulation
  • \\ — reverse solidus
  • \' — apostrophe
  • \xHH — byte with hexadecimal value HH

A character literal has type u8.

Parenthesized Expressions

A parenthesized expression is an expression enclosed in parentheses. It has the same type and value as the enclosed expression.

Syntax

primary_expression = integer_literal
                   | float_literal
                   | boolean_literal
                   | string_literal
                   | char_literal
                   | symbol_reference
                   | parenthesized_expression
                   | tuple_expression
                   | numeric_type
                   | boolean_type
                   | namespace_type
                   | type_type
                   | function_type
                   | match_expression
                   | scope_expression
                   | alloc_expression
                   | collect_expression ;

parenthesized_expression = left_paren, expression, right_paren ;

alloc_expression = alloc_handle_expression | alloc_slice_expression ;