Cargo build and environment variable

Hello all,

I have a rust binary (let's say binary_1) that can build a rust program (

In order to do that, the has:


It works fine ! and it build binary_2 !

Howevver, i would like binary_2 to have some optional parameter during build, like let's say an ip that could be different at each build. I don't want to pass the ip as a parameter to binary_2, but to be "hardcoded" into binary_2.

I thought about environment variable to be passed into cargo that specify the ip that has to be hardcoded into binary_2. I try with:


but obviously it doesn"t pass into binary_2 via cargo :stuck_out_tongue:

So i don't find how to do that.

Any idea?

You can add .env("FOO", "BAR") to your method chain before .spawn().

unfortunately it doesn't hardcode the value BAR into binary_2

How do you access FOO from binary_2? Do you use env!("FOO")?

1 Like

I access it with:

let ip = env::var("IP").unwrap();

I would like the binary to be portable . If i take the compiled version, it has to contains that ip, because of course the environment variable may not be present

std::env::var reads fresh values from the current runtime environment every time, not from the binary.

env!() macro reads values at compile time, which allows embedding them in the binary.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.