Skip to main content

Config

Derive Macro Config 

Source
#[derive(Config)]
{
    // Attributes available to this derive:
    #[config]
}
Available on crate feature macros only.
Expand description

Derive load() -> Result<Self, String> for a configuration struct.

Each field is bound to an environment variable. The env var name is derived from the field name (uppercased) plus an optional struct-level prefix.

§Struct-level attribute

#[config(prefix = "APP_")]

When set, every field’s env var key is prefix + key.

§Field-level attribute

#[config(env = "PORT", default = "8080")]
OptionMeaning
env = "KEY"explicit env var name (prefix is still prepended)
default = "v"fallback when the env var is absent

If env is omitted the field name is uppercased and used as the key. Wrapping the field type in Option<T> makes it optional (returns None when absent).

§Example

ⓘ
#[derive(rust_web_server::Config)]
#[config(prefix = "APP_")]
struct AppConfig {
    #[config(env = "PORT", default = "8080")]
    port: u16,
    #[config(env = "DATABASE_URL")]
    database_url: String,
    #[config(env = "DEBUG")]
    debug: Option<bool>,
}

let cfg = AppConfig::load().unwrap();