Constants and Static Items in Rust
A constant is a fixed value baked into the code wherever it is used; a static is a single value that lives at one address for the whole life of the program. Use a constant for a register offset or a device ID, and a static when something genuinely must be one shared thing. Because a shared mutable static could be written by two threads at once, Rust will not let you change one without extra protection.
Questions this Concept answers
- Why does Rust refuse to let safe code mutate a `static mut`?
Constants and Shadowing in Rust
Constants in Rust are values that are bound at compile time, must carry an explicit type annotation, and can never be reassigned, distinguishing them from static variables, which occupy a fixed memor…