3

I'd like to create a global static boolean value called IS_WINDOWS in a Rust file:

lazy_static! {
    pub static ref IS_WINDOWS: bool = std::env::consts::OS=="windows";
}

However, when I do this, anything that references the IS_WINDOWS value from elsewhere doesn't see it as a bool, they instead see it as a custom IS_WINDOWS struct, i.e. trying to do:

if crate::globals::IS_WINDOWS {
}

...results in error:

mismatched types
expected `bool`, found struct `globals::IS_WINDOWS`
LaVache
  • 2,372
  • 3
  • 24
  • 38

1 Answers1

3

Turns out, all I needed to do is use * to dereference the static variable:

if *crate::globals::IS_WINDOWS {
}
LaVache
  • 2,372
  • 3
  • 24
  • 38
  • 2
    I'll go ahead and link [this Q&A](https://stackoverflow.com/questions/48114390/why-does-a-lazy-static-value-claim-to-not-implement-a-trait-that-it-clearly-impl) explaining that lazy_static uses unique wrapper types for each variable, which is why the dereference is necessary. – kmdreko Jan 16 '21 at 18:14