I'm trying to use the thread-local LocalKey
to have a global game variable that the user can set once at the start of playing.
I finally got it to compile while setting a new PLAYER_NAME
in a with
block:
use std::thread::LocalKey;
use std::borrow::BorrowMut;
thread_local! {
pub static PLAYER_NAME: String = String::from("player-one");
}
fn main() {
let p: String = String::from("new-name");
PLAYER_NAME.with(|mut player_name| {
let player_name = p;
});
println!("PLAYER_NAME is: {:?}", PLAYER_NAME);
}
This prints out:
PLAYER_NAME is: LocalKey { .. }
How do I print the string value of PLAYER_NAME
? Do I have to use a with
block every time I want to read it too?