Constants and statics - Rust for C-Programmers
Constants represent values that are known at compile time. They must be annotated with a type and are typically defined in the global scope, though they can also be defined within functions. Constants are effectively inlined wherever they are used and do not have a fixed memory address. The naming convention is SCREAMING_SNAKE_CASE. Static variables represent values that have a fixed memory location ('static lifetime) throughout the program’s execution. They are initialized once, usually when the program starts. Like constants, they must have an explicit type annotation. The naming convention is also SCREAMING_SNAKE_CASE. Rust strongly discourages mutable static variables (static mut) because modifying global state without synchronization can easily lead to data races in concurrent code. Accessing or modifying static mut variables requires unsafe blocks.
Explore this link on the map →