29

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?

user3384741
  • 1,261
  • 3
  • 16
  • 21

2 Answers2

31

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);
}
Canato
  • 3,598
  • 5
  • 33
  • 57
Dogbert
  • 212,659
  • 41
  • 396
  • 397
0
use crate::VERSION
println!("version: {}", VERSION);

will be better.

chen Jacky
  • 507
  • 4
  • 8