Read environment variable

    0

    0

    Giovanny Gongora

    Codiga's Rust Recipes

    read or set fallback

    use std::env;
    use std::fs;
    use std::io::Error;
    
    fn main() -> Result<(), Error> {
      // read `config_path` from the environment variable `CONFIG`.
      // If `CONFIG` isn't set, fall back to a default config path.
      let config_path = env::var("CONFIG")
      .unwrap_or("/etc/myapp/config".to_string());
      let config: String = fs::read_to_string(config_path)?;
      println!("Config: {}", config);
      Ok(())
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.