API Reference

config-rs

Layered configuration library for Rust merging files, environment variables and defaults into a typed config.

Official Documentation ↗
CompatibilityThis documentation covers version config 0.14 / Rust 1.65+. Check the official docs for the latest API surface.

Installation

cargocargo add config

Methods

Overview

config-rs builds application configuration by layering sources such as files, environment variables and inline defaults, then exposing typed access or full deserialization into a struct.

Core Methods

Use Config::builder to add sources in precedence order, then read with typed getters or try_deserialize into a Serde struct. Prefixes and separators map environment variables to nested keys.

Usage Notes

Keep defaults in code or a base file, override with environment specifics, and deserialize into a strongly typed config passed to components. Fail fast on missing required values.

Common Pitfalls

Ambiguous precedence from unclear source order causes surprising values. Secrets leaking into logs via debug printing is a risk. Unvalidated config lets the app start in a broken state.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://docs.rs/config/latest/config/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:59.193Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

How is precedence determined?
Sources added later override earlier ones, so put defaults first and environment or secrets last. This ordering encodes the precedence policy explicitly.
How do environment variables map to keys?
With a prefix and separator configuration, APP_DB__HOST maps to db.host, allowing nested overrides from environment variables.
How do I validate required keys?
Deserialize into a struct with required fields or query specific keys and handle the error when missing, so the application fails fast on misconfiguration.