Constants
Constants store data that cannot be modified, just like immutable variables. However, unlike immutable variables:
-
Constants are declared using the
const
keyword -
Constants do not allow using the
mut
keyword (i.e., constant are not only immutable by default, but are always immutable) -
Constants require explicit type annotation
-
Constants must be set to a constant expressions (and not the result of a value that can only be computed at runtime)
Moreover:
-
Constants can be declared in the global scope
-
Constants use the
SCREAMING_SNAKE_CASE
form by convention
Example
// const mut ONE_HOUR_IN_SECONDS = 60*60; <-- fails to compile
const ONE_HOUR_IN_SECONDS: u32 = 3600;
fn main() {
assert!(ONE_HOUR_IN_SECONDS == 60*60);
}
You can experiment with this example in cairovm.codes and read more about constants in the Cairo Book. |