filipegoncalves / rust-config

Configuration file library in Rust similar to C/C++'s libconfig
MIT License
20 stars 10 forks source link

Is it possible to use a parsed Config struct in a static context? #3

Closed freiguy1 closed 9 years ago

freiguy1 commented 9 years ago

This may be a rust language question, rather than a question for you, but say I want to read in a config file and parse it once at the beginning of my program. Then at any time, I can get values from the pre-parsed Config by accessing it as a static struct. I'd rather not pass the config around everytime I create a new struct that might have use of it.

Thanks!

filipegoncalves commented 9 years ago

So, you mean use a Config as a global variable?

freiguy1 commented 9 years ago

Yes, exactly

filipegoncalves commented 9 years ago

This has been asked before on StackOverflow - see http://stackoverflow.com/questions/19605132/is-it-possible-to-use-global-variables-in-rust for further info, although the post is slightly outdated now: the most voted answer mentions the deprecated ~ pointer syntax, so take it with a grain of salt, but it looks to me like you want to do something similar to the db variable example in that answer.

Do keep in mind that it is generally bad practice to do so (and I believe that's why Rust makes it non-trivial).

I also checked the Rust documentation; it briefly mentions global variables here.

Maybe you could design your program so as to keep references to the Config instance where necessary?

freiguy1 commented 9 years ago

Thanks for the information. I'll try to design the program without global variables.