Search code examples
rustrust-cargo

Rust use different variable value for Cargo profile


This is surprisingly hard to find in the documentation. I want to use a different base_url for my API endpoint for every Cargo profile. So, —-profile=debug will use https://localhost:3000, and —-profile=release will use https://api.cooldomain.io/ or something like that.

Any pointers?


Solution

  • You can set your base_url using #[cfg(debug_assertions)] for debug profiles and using #[cfg(not(debug_assertions))] for non debug profiles.

    For example like this

    #[cfg(debug_assertions)]
    const BASE_URL:&'static str = "http://localhost:3000";
    
    #[cfg(not(debug_assertions))]
    const BASE_URL:&'static str = "https://api.cooldomain.io/";
    
    fn main() {
        println!("{}",BASE_URL);
    }
    

    You can read more about debug_assertions here