Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Constants

Declaration

<constant_declaration> ::= "const" <identifier> [":" <type_signature>] ;

Dependencies:

  • <identifier>
  • <type_signature>

The <constant_declaration> maps to ConstDecl { name, ty: Option<TypeSig>, span }.

Assignment

<constant_assignment> ::= <constant_declaration> "=" <expression> ";" ;

Dependencies:

  • <expression>

The <constant_assignment> produces Stmt::ConstAssign(ConstDecl, Expr, Span).

Semantics

Constants must be resolvable at compile time by assigning a literal, another constant, or an expression that can be evaluated at compile time.

The type of a constant is inferred from its assignment when no explicit type annotation is provided.

const A: u8 = 1;
const B = 1;
const C = B;
const D = a();
 
comptime fn a() -> u8 {
    1
}