Google News
logo
Rust - Interview Questions
How to declare global variables in Rust?
In Rust, you can declare a global variable using the static keyword. The static keyword declares a global variable with a static lifetime, which means that it exists for the entire duration of the program's execution.

To declare a global variable, you need to specify the type, and it must have a constant expression for initialization. Additionally, since global variables can be accessed from multiple threads, one must ensure to handle synchronization when using mutable global variables.
Advertisement