skip to Main Content

I added a new environment variable into ~/.zshrc in macOS 13.2 few minites ago, this is the content about the variable in .zshrc:

export ALT_DATABASE_URL=postgres://postgres:[email protected]:5432/alt

then run the source command to make it works:

source ~/.zshrc

check the variable:

> env|grep "DATABASE_URL"
DATABASE_URL=postgres://postgres:[email protected]:5432/dolphin
CV_DATABASE_URL=postgres://postgres:[email protected]:5432/cv
ALT_DATABASE_URL=postgres://postgres:[email protected]:5432/alt

when I using this rust code to read the environment variable in visual studio code with lldb plugin:

fn main() {
    let cv = std::env::var("CV_DATABASE_URL").expect("get cv url error");
    print!("cv:{}", cv);
    let alt = std::env::var("ALT_DATABASE_URL").expect("get alt url error");
    print!("alt:{}", alt);
}

the newly add variable read failed:

thread 'main' panicked at 'get alt url error: NotPresent', src/main.rs:4:49
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
cv:postgres://postgres:[email protected]:5432/cv%                                                                                             

am I missing something? what should I do to fixed this issue? I have do some investgate seems the visual studio code lldb plugin could not read the os environment variable.

2

Answers


  1. Chosen as BEST ANSWER

    Finally I found a way to manage the rust environment varialbe, on the visual studio code launch.json, add the envFile configuration:

    "envFile": "${workspaceFolder}/.env"
    

    then in the project folder add an .env file and add the environment key value into .env like this:

    KEY1=VALUE1
    

    also you can choose the dotenv package but I found this repo stop maintain for 3 years, not recommend.


  2. This is more a shell question than a Rust question, but the variable must be exported to be seen in child processes.

    So run

    export ALT_DATABASE_URL
    

    before running your program.

    You can also set and export at the same time:

    export ALT_DATABASE_URL=whatever
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search