.. default-domain:: spec
Legality Rules
:dp:`fls_buyaqara7am4` 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.
:dp:`fls_CUJyMj0Sj8NS` An :dt:`allocated object` is a value stored at some memory address.
:dp:`fls_kaomYy0Ml4Nh` The :dt:`base address` of an allocated object is the memory address where the object is stored.
:dp:`fls_B5cmkWfD5GNt` An [allocated object]s :dt:`memory size` is the number of bytes the object spans in memory from its base address.
:dp:`fls_rixdyyc525xp` Two [value]s overlap when
- :dp:`fls_m6ctqq70vcxr` Both [value]s are the same, or
- :dp:`fls_s231d18x5eay` One value is of an abstract data type and the other denotes a field of the same value, or
- :dp:`fls_dfr4yqo93fsn` One value denotes an array and the other denotes an element of the same value, or
- :dp:`fls_eoak5mdl6ma` Both [value]s are elements of the same array.
Undefined Behavior
:dp:`fls_6lg0oaaopc26` It is undefined behavior to create a value from uninitialized memory unless the type of the value is a union type.
:dp:`fls_oqhQ62mDLckN` It is undefined behavior to create an allocated object at base address :c:`null`.
:dp:`fls_uhwpuv6cx4ip`
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 :c:`usize` value.
:dp:`fls_xuuFKmm181bs`
It is undefined behavior to create an allocated object with memory
size size where size is greater than the architectures maximum
:c:`isize` value.
Syntax
.. syntax::
ConstantDeclaration ::=
$$const$$ (Name | $$_$$) TypeAscription ConstantInitializer? $$;$$
ConstantInitializer ::=
$$=$$ Expression
Legality Rules
:dp:`fls_5o5iu4j8in4l` A constant is an immutable value expression whose uses are substituted by the value.
:dp:`fls_3mhj0kkupwuz` An unnamed constant is a constant declared with character 0x5F (low line).
:dp:`fls_ka4y2yd100dx`
The type specification of a constant shall have 'static
lifetime.
:dp:`fls_vt9tlkd676ql` The type of a constant shall implement the :std:`core::marker::Sized` trait.
:dp:`fls_ooOYxhVh8hZo` The type of a constant cannot be a mutable reference type.
:dp:`fls_ndmfqxjpvsqy` A constant initializer is a construct that provides the value of its related constant.
:dp:`fls_6rxwbbhf5tc5` A constant shall have a constant initializer, unless it is an associated trait constant.
:dp:`fls_vnc3ttnid1qr` The expression of a constant initializer shall be a constant expression.
:dp:`fls_deuo1pn8cjd6` The value of a constant is determined by evaluating its constant initializer.
Dynamic Semantics
:dp:`fls_xezt9hl069h4` The elaboration of a constant evaluates its constant initializer.
:dp:`fls_ndobth7s92if` A path that refers to a constant is replaced with the value of the constant.
Examples
const ZERO: u32 = 0;Syntax
.. syntax::
StaticDeclaration ::=
ItemSafety? $$static$$ $$mut$$? Name TypeAscription StaticInitializer? $$;$$
StaticInitializer ::=
$$=$$ Expression
Legality Rules
:dp:`fls_ibrmiwfypldh` A static is a value that is associated with a specific memory location.
:dp:`fls_mt94jvoot9dx` A static defined within a generic function exists once in the output executable or library.
:dp:`fls_k0r2c6uq29tu`
The type specification of a static shall have 'static
lifetime.
:dp:`fls_b6ods85htuyn` The type of a static shall implement the :std:`core::marker::Sized` trait.
:dp:`fls_WRpcVF1fLEpr` A static shall only be subject to an :s:`ItemSafety` if it is an external static in an unsafe external block.
:dp:`fls_doi4z6u55bi7`
A mutable static is a static with keyword mut whose
value can be modified.
:dp:`fls_74hp208pto22` Access to a mutable static shall require unsafe context.
:dp:`fls_jfde2vg6mtww` An immutable static is a static whose value cannot be modified.
:dp:`fls_k4tyqb1j6zjo` The type of an immutable static shall implement the :std:`core::marker::Sync` trait.
:dp:`fls_t17h5h6a6v4c` A static initializer is a construct that provides the value of its related static.
:dp:`fls_yq0hpy4jx2qb` A static shall have a static initializer, unless it is an external static.
:dp:`fls_vgidvfwzm4ks` The expression of a static initializer shall be a constant expression.
:dp:`fls_8dcldbvu7lav` A use of a static is a place expression referring to the unique location of the static.
Dynamic Semantics
:dp:`fls_w0nb0mphho7b` The elaboration of a static evaluates its static initializer.
:dp:`fls_eeocxst9vafn` All [path]s that refer to a static refer to the same memory location.
:dp:`fls_47khd5ljsxeq` A static is not dropped during destruction.
:dp:`fls_dowxbphqvk3n` An immutable static whose type is not subject to interior mutability may reside in read-only memory.
Undefined Behavior
:dp:`fls_b5wsmii7vz3v` It is undefined behavior to mutate an immutable static whose type is not subject to interior mutability.
Examples
static mut GLOBAL: u32 = 0;Legality Rules
:dp:`fls_awpw61yofckz` A temporary is an anonymous variable produced by some intermediate computation.
Legality Rules
:dp:`fls_hl5tnd9yy252` A variable is a placeholder for a value that is allocated on the stack.
:dp:`fls_vgi0gh5zmoiu` The following [construct]s are [variable]s:
- :dp:`fls_81dlbula47nu` A binding.
- :dp:`fls_3p0sb9ppmg3w` A temporary.
:dp:`fls_r9km9f969bu8` A variable shall be used only after it has been initialized through all [reachable control flow path]s up to the point of its usage.
Dynamic Semantics
:dp:`fls_g8etd5lsgn9j` A variable is not initialized when allocated.
Legality Rules
:dp:`fls_udn9lyf3m0z6` Constant promotion is the process of converting a value expression into a constant.
:dp:`fls_yvkdcs4pmxjf` Constant promotion is possible only when
- :dp:`fls_n570za6a9nqd` The value expression is a constant expression, and
- :dp:`fls_tms5r9f5ogcb` The type of the value expression does not have a destructor, and
- :dp:`fls_bysv5r7iuf5j` The value expression does not employ a struct expression constructing a :std:`core::cell::UnsafeCell`, and
- :dp:`fls_3h5vr7xk2rrt` The value expression only consists of operations that will always succeed evaluation, and
- :dp:`fls_3BGncWvMumEt` The value expression is the operand of an immutable borrow expression.
:dp:`fls_m690b8qg9d9r`
Constant promotion is always possible for expression &mut [],
promoting the produced mutable borrow to have 'static lifetime.
:dp:`fls_uf0sg25awre6` Constant promotion proceeds as follows:
- :dp:`fls_o7cqfdnr253y` An anonymous constant is created, whose constant initializer holds the result of the value expression.
- :dp:`fls_ap85svxyuhvg`
The value of the anonymous constant is borrowed with
'staticlifetime.