How do you do the argument parsing? Maybe you could show us some more of your main() function. The main() function is the entry point for your program, it is the function that does the argument parsing, too, in the first place, as well as displaying the help message; so to “guarantee that nothing from the main() will run before the help” does – technically – not make too much sense. Depending on how you do argument parsing, it might be straightforward to make that happen before you create the GlobalState with its Spinner.
Dereferencing the lazy_static to make sure its creation (and hence the help message being displayed and the program exiting) happens before anything else in main.
For a given static ref NAME: TYPE = EXPR;, the macro generates a unique type that implements Deref<TYPE> and stores it in a static with name NAME . (Attributes end up attaching to this type.)
On first deref, EXPR gets evaluated and stored internally, such that all further derefs can return a reference to the same object.
You are suggesting total rewrite. Besides, ARGS is read only, treated like a constant. It is not bad style to use a constant throughout your code. I'd like to get away with it
Well, yes. But, from what I've seen you've already burned yourself (at least) twice with the issue of a global variable. So you should probably learn to avoid them where possible, and here the best practice would not be to parse arguments globally.
Sorry, I'm not getting it so fast. I tried your suggestion literally:
fn main() {
*ARGS; // make sure `ARGS` is initialized after this point
let mut g = GlobalState {
spinner: Spinner::new(),
...
Got an error:
error[E0507]: cannot move out of dereference of `ARGS`
--> src/main.rs:816:5
|
816 | *ARGS; // make sure `ARGS` is initialized after this point
| ^^^^^ move occurs because value has type `ArgMatches<'_>`, which does not implement the `Copy` trait