Logo

Using envvault with Rust

Note: Please ensure that you have completed the previous steps

Prerequisites

  • Rust and Cargo installed
  • envvault CLI tool installed
  • An existing Rust project

Usage

Running Your Application

To run your Rust application with environment variables from envvault:

$ envvault run --env=dev cargo run

Caching Environment Variables

For better performance, you can cache your environment variables:

$ envvault run --env=dev -c -- cargo run

Example Implementation

Here is how to set up a basic Rust web server with envvault:

// main.rs
use actix_web::{web, App, HttpServer, HttpResponse};
use std::env;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Your environment variables are automatically loaded
    let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());
    let db_url = env::var("DB_URL").expect("DB_URL must be set");

    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(|| async { 
                HttpResponse::Ok().body("Hello from envvault!")
            }))
    })
    .bind(format!("127.0.0.1:{}", port))?
    .run()
    .await
}

// Cargo.toml
[dependencies]
actix-web = "4.0"