Rust constants in different modules?


I have this "main.rs" file which I declare a version constant.

pub const VERSION: &'static str = "v2";
mod game;
fn main() {
   do_stuff();
}

Then I want to access this global constant in a different module "game.rs":

pub fn do_stuff() {
   println!("This is version: {}", VERSION);
}

How do I make the constant available everywhere?



As VERSION is declared in main.rs, which is a crate root, you can access it using its absolute path: ::VERSION.

This should work:

pub fn do_stuff() {
    println!("This is version: {}", crate::VERSION);
}