Avatar ·

Can I use global variables in Rust?

Hi everyone! I just started learning Rust, coming from C++, and I ran into a problem. In C++, I'm used to using global variables to store application configuration or counters that need to be accessed from different functions. For example, I have a simple function call counter: ```rust static COUNTER: i32 = 0; fn increment() { COUNTER += 1; // Error! } ``` The compiler gives an error: "cannot mutate static variable". I tried wrapping it in `mut`: ```rust static mut COUNTER: i32 = 0; fn increment() { COUNTER += 1; // unsafe! Very inconvenient. } ``` Now the compiler requires wrapping the access in `unsafe { ... }`. I understand that this is unsafe, but how do I properly organize global state? I need the variable to be visible throughout the entire program.