1

I want to be able to provide a variable via a command line at compile time

MY_VAR="my_var1" cargo build

# or
MY_VAR="my_var111" cargo build

# or
MY_VAR="my_varfdsafdsafds" cargo build

and a library should be compiled with the value of a variable and see that value in its code. A library must not resolve a variable at runtime, but at compile time.

I don't consider changing the code of a library in any way, nor .cargo/config, nor Cargo.toml

How can I do it? Is it possible?

mixtouku
  • 67
  • 1
  • 3

1 Answers1

2

You can use the env!() macro to get the value of an environment variable at compile time:

let my_var: &'static str = env!("MY_VAR");

This will embed the value MY_VAR had during compilation into the binary, so it's available as a &'static str at runtime.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841