Search code examples
ruststaticbooleangloballazy-static

Define a static boolean in Rust from std::env::consts::OS=="windows"


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`

Solution

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

    if *crate::globals::IS_WINDOWS {
    }