I'm learning Rust, and writing a command line program. I'm parsing the command line arguments into a struct with the
structopt crate, and now I want to store that struct somewhere so I can read its members from other places in the program. In any other language that somewhere would be a global variable, but those are tricky in Rust.
So what is the idiomatic way to store a write-once read-many object like this? I could use a
static mut, but then I'm immediately resigning to
unsafe code. I don't want to pass a reference to the struct everywhere throughout the program either. There are also a number of ways using macros such as
thread_local. But I'm not sure which of these would be idiomatic in a case like mine. I'm sure this use case has come up many times before.