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 An allocated object is a value stored at some memory address.

7:3 An allocated objects base address is the the memory address the object is stored.

7:4 An allocated objects memory size is the number of bytes the object spans in memory from its base address.

7:5 Two values overlap when

Undefined Behavior

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

7:11 It is undefined behavior to create an allocated object at base address null.

7:12 It is undefined behavior to create an allocated object with memory size size at a base address base where base + size is greater than the architectures maximum usize value.

7:13 It is undefined behavior to create an allocated object with memory size size where size is greater than the architectures maximum isize value.

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 ::=
    ItemSafety? 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 static shall only be subject to an ItemSafety if it is an external static in an unsafe external block.

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

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

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

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

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

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

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

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

Dynamic Semantics

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

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

7.2:16 A static is not dropped during destruction.

7.2:17 An immutable static whose type is not subject to interior mutability may reside in read-only memory.

Undefined Behavior

7.2:18 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.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy