7. Values

Legality Rules

7:1 A value is either a literal or the result of a computation, that may be stored in a memory location, and interpreted based on some type.

7:2 Two values overlap when

Undefined Behavior

7:7 It is undefined behavior to create a value from uninitialized memory unless the type of the value is a union type.

7.1. Constants

Syntax

ConstantDeclaration ::=
    const (Name | _) TypeAscription ConstantInitializer? ;

ConstantInitializer ::=
    = Expression

Legality Rules

7.1:1 A constant is an immutable value whose uses are substituted by the value.

7.1:2 An unnamed constant is a constant declared with character 0x5F (low line).

7.1:3 The type specification of a constant shall have 'static lifetime.

7.1:4 The type of a constant shall implement the core::marker::Sized trait.

7.1:5 A constant initializer is a construct that provides the value of its related constant.

7.1:6 A constant shall have a constant initializer, unless it is an associated trait constant.

7.1:7 The expression of a constant initializer shall be a constant expression.

7.1:8 The value of a constant is determined by evaluating its constant initializer.

7.1:9 A use of a constant is a value expression and creates a copy of the constant’s value.

Dynamic Semantics

7.1:10 The elaboration of a constant evaluates its constant initializer.

7.1:11 A path that refers to a constant is replaced with the value of the constant.

Examples

const ZERO: u32 = 0;

7.2. Statics

Syntax

StaticDeclaration ::=
    static mut? Name TypeAscription StaticInitializer? ;

StaticInitializer ::=
    = Expression

Legality Rules

7.2:1 A static is a value that is associated with a specific memory location.

7.2:2 A static defined within a generic function exists once in the output executable or library.

7.2:3 The type specification of a static shall have 'static lifetime.

7.2:4 The type of a static shall implement the core::marker::Sized trait.

7.2:5 A mutable static is a static with keyword mut whose value can be modified.

7.2:6 Access to a mutable static shall require unsafe context.

7.2:7 An immutable static is a static whose value cannot be modified.

7.2:8 The type of an immutable static shall implement the core::marker::Sync trait.

7.2:9 A static initializer is a construct that provides the value of its related static.

7.2:10 A static shall have a static initializer, unless it is an external static.

7.2:11 The expression of a static initializer shall be a constant expression.

7.2:12 A use of a static is a place expression referring to the unique location of the static.

Dynamic Semantics

7.2:13 The elaboration of a static evaluates its static initializer.

7.2:14 All paths that refer to a static refer to the same memory location.

7.2:15 A static is not dropped during destruction.

7.2:16 A mutable static whose type is not subject to interior mutability may reside in read-only memory.

Undefined Behavior

7.2:17 It is undefined behavior to mutate an immutable static whose type is not subject to interior mutability.

Examples

static mut GLOBAL: u32 = 0;

7.3. Temporaries

Legality Rules

7.3:1 A temporary is an anonymous variable produced by some intermediate computation.

7.4. Variables

Legality Rules

7.4:1 A variable is a placeholder for a value that is allocated on the stack.

7.4:2 The following constructs are variables:

7.4:5 A variable shall be used only after it has been initialized through all reachable control flow paths up to the point of its usage.

Dynamic Semantics

7.4:6 A variable is not initialized when allocated.

7.4.1. Constant Promotion

Legality Rules

7.4.1:1 Constant promotion is the process of converting a value expression into a constant.

7.4.1:2 Constant promotion is possible only when

7.4.1:8 Constant promotion is always possible for expression &mut [], promoting the produced mutable borrow to have 'static lifetime.

7.4.1:9 Constant promotion proceeds as follows:

  1. 7.4.1:10 An anonymous constant is created, whose constant initializer holds the result of the value expression.

  2. 7.4.1:11 The value of the anonymous constant is borrowed with 'static lifetime.