Printing something created by lazy_static

I think I outsmarted myself. I have a config object that I create with lazy_static.

    pub static ref CONFIG: Config = Config::new().unwrap();
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
   pub var1: usize,
   pub var2: String,
   // A whole lot more
impl Config {
    pub fn new() -> Result<Config, Error> {
        // I actually get the file name from args
        let file = OpenOptions::new().read(true).open("model1.config")?;
        let config: Config = serde_json::from_reader(file)?;

In the rest of my code I can access configuration variables as CONFIG.var1.

I just got a bug report from someone who didn't remember which configuration he used. I decided it might be a good idea to include CONFIG in our traces. That didn't work because CONFIG isn't Serialize. Next, I tried to print CONFIG to the console, but it isn't Display or Debug.

My solution is to re-read the config file in and add it to the trace file there. It seems there should be a better way.

CONFIG isn't Debug or Serialize, but *CONFIG is.

fn f() {
    println!("{:?}", *CONFIG);  // works

fn g() -> String {
    serde_json::to_string(&*CONFIG).unwrap()  // also works
1 Like

Thanks. I never would have thought to try that. Of course, now that I see the solution, I can see why it works.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.